/
pdfwrite.cpp
180 lines (149 loc) · 4.95 KB
/
pdfwrite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, chch-kiwi@users.sourceforge.net
.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
#include <QDebug>
#include "podofo/podofo.h"
#include "err.h"
#include "file.h"
#include "pdfwrite.h"
using namespace PoDoFo;
Pdfwrite::Pdfwrite (const QString &fname)
{
_doc = 0;
_fname = fname;
}
Pdfwrite::~Pdfwrite ()
{
if (_doc)
delete _doc;
}
err_info *Pdfwrite::open (void)
{
try
{
_doc = new PdfMemDocument ();
_doc->Load (_fname.latin1 ());
}
catch (const PdfError &eCode)
{
return err_make (ERRFN, E_pdf_creation_error1, eCode.ErrorMessage (eCode.GetError()));
}
return NULL;
}
err_info *Pdfwrite::close (void)
{
try
{
_doc->Write (_fname.latin1 ());
}
catch (const PdfError &eCode)
{
return err_make (ERRFN, E_pdf_creation_error1, eCode.ErrorMessage (eCode.GetError()));
}
return NULL;
}
err_info *Pdfwrite::create (void)
{
try
{
qDebug () << _fname;
_doc = new PdfMemDocument ();
_doc->GetInfo()->SetCreator ( PdfString("examplahelloworld - A PoDoFo test application") );
_doc->GetInfo()->SetAuthor ( PdfString("Dominik Seichter") );
_doc->GetInfo()->SetTitle ( PdfString("Hello World") );
_doc->GetInfo()->SetSubject ( PdfString("Testing the PoDoFo PDF Library") );
_doc->GetInfo()->SetKeywords( PdfString("Test;PDF;Hello World;") );
}
catch (const PdfError &eCode)
{
return err_make (ERRFN, E_pdf_creation_error1, eCode.ErrorMessage (eCode.GetError()));
}
#if 0
try
{
_doc = new PdfStreamedDocument (_fname.latin1 ());
PdfPage* pPage;
PdfPainter painter;
PdfFont* pFont;
pPage = _doc->CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );
if( !pPage )
{
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
painter.SetPage( pPage );
pFont = _doc->CreateFont( "Arial" );
if( !pFont )
{
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
pFont->SetFontSize( 18.0 );
painter.SetFont( pFont );
painter.DrawText( 56.69, pPage->GetPageSize().GetHeight() - 56.69, "Hello World!" );
painter.FinishPage();
_doc->GetInfo()->SetCreator ( PdfString("examplahelloworld - A PoDoFo test application") );
_doc->GetInfo()->SetAuthor ( PdfString("Dominik Seichter") );
_doc->GetInfo()->SetTitle ( PdfString("Hello World") );
_doc->GetInfo()->SetSubject ( PdfString("Testing the PoDoFo PDF Library") );
_doc->GetInfo()->SetKeywords( PdfString("Test;PDF;Hello World;") );
_doc->Close();
}
catch (const PdfError &eCode)
{
return err_make (ERRFN, E_pdf_creation_error1, eCode.ErrorMessage (eCode.GetError()));
}
#endif
return NULL;
}
err_info *Pdfwrite::addPage (const Filepage *mp)
{
try
{
Q_ASSERT (_doc);
// _doc = new PdfMemDocument (_fname.latin1 ());
PdfPage *page;
PdfPainter painter;
page = _doc->CreatePage (PdfPage::CreateStandardPageSize (ePdfPageSize_A4));
if (!page)
PODOFO_RAISE_ERROR (ePdfError_InvalidHandle);
painter.SetPage (page);
PdfImage *image = new PdfImage (_doc);
QByteArray ba = mp->invertData ();
PdfMemoryInputStream input (ba.constData (), mp->_size);
image->SetImageColorSpace (mp->_depth <= 8 ? ePdfColorSpace_DeviceGray
: ePdfColorSpace_DeviceRGB);
image->SetImageData (mp->_width, mp->_height, mp->_depth == 1 ? 1 : 8, &input);
PdfRect rect = page->GetPageSize ();
double w = image->GetWidth ();
double h = image->GetHeight ();
double xscale = rect.GetWidth () / w;
double yscale = rect.GetHeight () / h;
double scale = xscale;
if (scale > yscale)
scale = yscale;
painter.DrawImage (rect.GetLeft (), rect.GetBottom (), image, scale, scale);
painter.FinishPage();
}
catch (const PdfError &eCode)
{
return err_make (ERRFN, E_pdf_creation_error1, eCode.ErrorMessage (eCode.GetError()));
}
return NULL;
}