@@ -65,8 +65,6 @@ extern "C" pdf_t * pdf_load_template(const char *filename)
65
65
unsigned pages = (pdf->getAllPages ()).size ();
66
66
67
67
if (pages != 1 ) {
68
- fprintf (stderr, " ERROR: PDF template must contain exactly 1 page: %s\n " ,
69
- filename);
70
68
delete pdf;
71
69
return NULL ;
72
70
}
@@ -139,26 +137,21 @@ int pdf_pages_fp(FILE *file)
139
137
* I - buffer containing data to be prepended
140
138
* I - length of buffer
141
139
*/
142
- extern " C" void pdf_prepend_stream (pdf_t *pdf,
143
- unsigned page_num,
144
- char const *buf,
145
- size_t len)
140
+ extern " C" int pdf_prepend_stream (pdf_t *pdf,
141
+ unsigned page_num,
142
+ char const *buf,
143
+ size_t len)
146
144
{
147
145
std::vector<QPDFObjectHandle> pages = pdf->getAllPages ();
148
- if (pages.empty () || page_num > pages.size ()) {
149
- fprintf (stderr, " ERROR: Unable to prepend stream to requested PDF page\n " );
150
- return ;
151
- }
146
+ if (pages.empty () || page_num > pages.size ())
147
+ return (1 );
152
148
153
149
QPDFObjectHandle page = pages[page_num - 1 ];
154
150
155
151
// get page contents stream / array
156
152
QPDFObjectHandle contents = page.getKey (" /Contents" );
157
153
if (!contents.isStream () && !contents.isArray ())
158
- {
159
- fprintf (stderr, " ERROR: Malformed PDF.\n " );
160
- return ;
161
- }
154
+ return (1 );
162
155
163
156
// prepare the new stream which is to be prepended
164
157
PointerHolder<Buffer> stream_data = PointerHolder<Buffer>(new Buffer (len));
@@ -177,6 +170,8 @@ extern "C" void pdf_prepend_stream(pdf_t *pdf,
177
170
178
171
contents.insertItem (0 , stream);
179
172
page.replaceKey (" /Contents" , contents);
173
+
174
+ return (0 );
180
175
}
181
176
182
177
@@ -187,24 +182,19 @@ extern "C" void pdf_prepend_stream(pdf_t *pdf,
187
182
* I - page number of the page to which the font is to be added
188
183
* I - name of the font to be added
189
184
*/
190
- extern " C" void pdf_add_type1_font (pdf_t *pdf,
191
- unsigned page_num,
192
- const char *name)
185
+ extern " C" int pdf_add_type1_font (pdf_t *pdf,
186
+ unsigned page_num,
187
+ const char *name)
193
188
{
194
189
std::vector<QPDFObjectHandle> pages = pdf->getAllPages ();
195
- if (pages.empty () || page_num > pages.size ()) {
196
- fprintf (stderr, " ERROR: Unable to add type1 font to requested PDF page\n " );
197
- return ;
198
- }
190
+ if (pages.empty () || page_num > pages.size ())
191
+ return (1 );
199
192
200
193
QPDFObjectHandle page = pages[page_num - 1 ];
201
194
202
195
QPDFObjectHandle resources = page.getKey (" /Resources" );
203
196
if (!resources.isDictionary ())
204
- {
205
- fprintf (stderr, " ERROR: Malformed PDF.\n " );
206
- return ;
207
- }
197
+ return (1 );
208
198
209
199
QPDFObjectHandle font = QPDFObjectHandle::newDictionary ();
210
200
font.replaceKey (" /Type" , QPDFObjectHandle::newName (" /Font" ));
@@ -218,14 +208,13 @@ extern "C" void pdf_add_type1_font(pdf_t *pdf,
218
208
fonts = QPDFObjectHandle::newDictionary ();
219
209
}
220
210
else if (!fonts.isDictionary ())
221
- {
222
- fprintf (stderr, " ERROR: Can't recognize Font resource in PDF template.\n " );
223
- return ;
224
- }
211
+ return (1 );
225
212
226
213
font = pdf->makeIndirectObject (font);
227
214
fonts.replaceKey (" /bannertopdf-font" , font);
228
215
resources.replaceKey (" /Font" , fonts);
216
+
217
+ return (0 );
229
218
}
230
219
231
220
@@ -306,27 +295,23 @@ static void fit_rect(float oldrect[4],
306
295
* I - Length of page to set
307
296
* I - Scale of page to set
308
297
*/
309
- extern " C" void pdf_resize_page (pdf_t *pdf,
310
- unsigned page_num,
311
- float width,
312
- float length,
313
- float *scale)
298
+ extern " C" int pdf_resize_page (pdf_t *pdf,
299
+ unsigned page_num,
300
+ float width,
301
+ float length,
302
+ float *scale)
314
303
{
315
304
std::vector<QPDFObjectHandle> pages = pdf->getAllPages ();
316
- if (pages.empty () || page_num > pages.size ()) {
317
- fprintf (stderr, " ERROR: Unable to resize requested PDF page\n " );
318
- return ;
319
- }
305
+ if (pages.empty () || page_num > pages.size ())
306
+ return (1 );
320
307
321
308
QPDFObjectHandle page = pages[page_num - 1 ];
322
309
float new_mediabox[4 ] = { 0.0 , 0.0 , width, length };
323
310
float old_mediabox[4 ];
324
311
QPDFObjectHandle media_box;
325
312
326
- if (!dict_lookup_rect (page, " /MediaBox" , old_mediabox, true )) {
327
- fprintf (stderr, " ERROR: pdf doesn't contain a valid mediabox\n " );
328
- return ;
329
- }
313
+ if (!dict_lookup_rect (page, " /MediaBox" , old_mediabox, true ))
314
+ return (1 );
330
315
331
316
fit_rect (old_mediabox, new_mediabox, scale);
332
317
media_box = makeRealBox (new_mediabox);
@@ -336,6 +321,8 @@ extern "C" void pdf_resize_page (pdf_t *pdf,
336
321
page.replaceKey (" /CropBox" , media_box);
337
322
page.replaceKey (" /MediaBox" , media_box);
338
323
page.replaceKey (" /TrimBox" , media_box);
324
+
325
+ return (0 );
339
326
}
340
327
341
328
@@ -345,22 +332,22 @@ extern "C" void pdf_resize_page (pdf_t *pdf,
345
332
* I - page number of the page to be duplicated
346
333
* I - number of copies to be duplicated
347
334
*/
348
- extern " C" void pdf_duplicate_page (pdf_t *pdf,
349
- unsigned page_num,
350
- unsigned count)
335
+ extern " C" int pdf_duplicate_page (pdf_t *pdf,
336
+ unsigned page_num,
337
+ unsigned count)
351
338
{
352
339
std::vector<QPDFObjectHandle> pages = pdf->getAllPages ();
353
- if (pages.empty () || page_num > pages.size ()) {
354
- fprintf (stderr, " ERROR: Unable to duplicate requested PDF page\n " );
355
- return ;
356
- }
340
+ if (pages.empty () || page_num > pages.size ())
341
+ return (1 );
357
342
358
343
QPDFObjectHandle page = pages[page_num - 1 ];
359
344
for (unsigned i = 0 ; i < count; ++i)
360
345
{
361
346
page = pdf->makeIndirectObject (page);
362
347
pdf->addPage (page, false );
363
348
}
349
+
350
+ return (0 );
364
351
}
365
352
366
353
@@ -408,7 +395,7 @@ std::string lookup_opt(opt_t *opt, std::string const& key) {
408
395
* 3. Fill recognized fields with information.
409
396
* I - Pointer to the QPDF structure
410
397
* I - Pointer to the opt_t type list
411
- * O - status of form fill - 0 for failure , 1 for success
398
+ * O - status of form fill - 0 for success , 1 for failure
412
399
*/
413
400
extern " C" int pdf_fill_form (pdf_t *doc, opt_t *opt)
414
401
{
@@ -418,19 +405,15 @@ extern "C" int pdf_fill_form(pdf_t *doc, opt_t *opt)
418
405
QPDFPageDocumentHelper pdh (*doc);
419
406
420
407
// check if the PDF has a form or not
421
- if ( !afdh.hasAcroForm () ) {
422
- fprintf (stderr, " DEBUG: PDF template file doesn't have form. It's okay.\n " );
423
- return 0 ;
424
- }
408
+ if (!afdh.hasAcroForm ())
409
+ return 1 ;
425
410
426
411
// get the first page from the PDF to fill the form. Since this
427
412
// is a banner file,it must contain only a single page, and that
428
413
// check has already been performed in the `pdf_load_template()` function
429
414
std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages ();
430
- if (pages.empty ()) {
431
- fprintf (stderr, " ERROR: Can't get page from PDF tamplate file.\n " );
432
- return 0 ;
433
- }
415
+ if (pages.empty ())
416
+ return 1 ;
434
417
QPDFPageObjectHelper page = pages.front ();
435
418
436
419
// get the annotations in the page
@@ -467,7 +450,7 @@ extern "C" int pdf_fill_form(pdf_t *doc, opt_t *opt)
467
450
}
468
451
}
469
452
470
- // status 1 notifies that the function successfully filled all the
453
+ // status 0 notifies that the function successfully filled all the
471
454
// identifiable fields in the form
472
- return 1 ;
455
+ return 0 ;
473
456
}
0 commit comments