Skip to content

Commit 60ed1fa

Browse files
committed
libcupsfilters: Fixed logging in the bannertopdf() filter function
The bannertopdf() filter function uses the functions in cupsfilters/pdf.cxx and these emit various error messages directly to stderr. Now the functions in cupsfilters/pdf.cxx are changed to return a success/failure status and do not log any more. Instead the bannertopdf() filter function logs failures of these functions via the log function.
1 parent c892377 commit 60ed1fa

File tree

3 files changed

+89
-78
lines changed

3 files changed

+89
-78
lines changed

cupsfilters/bannertopdf.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#endif /* HAVE_CUPS_1_7 */
4242

4343
#include "filter.h"
44-
#include <cupsfilters/pdf.h>
44+
#include "pdf.h"
4545

4646
enum banner_info
4747
{
@@ -748,14 +748,31 @@ static int generate_banner_pdf(banner_t *banner,
748748
#endif
749749

750750
if (!(doc = pdf_load_template(banner->template_file)))
751-
return 1;
751+
{
752+
if (log) log(ld, FILTER_LOGLEVEL_ERROR,
753+
"PDF template must contain exactly 1 page: %s",
754+
banner->template_file);
755+
return (1);
756+
}
752757

753758
get_pagesize(data, noptions, options,
754759
&page_width, &page_length, media_limits);
755760

756-
pdf_resize_page(doc, 1, page_width, page_length, &page_scale);
761+
if (pdf_resize_page(doc, 1, page_width, page_length, &page_scale) != 0)
762+
{
763+
if (log) log(ld, FILTER_LOGLEVEL_ERROR,
764+
"Unable to resize requested PDF page");
765+
pdf_free(doc);
766+
return (1);
767+
}
757768

758-
pdf_add_type1_font(doc, 1, "Courier");
769+
if (pdf_add_type1_font(doc, 1, "Courier") != 0)
770+
{
771+
if (log) log(ld, FILTER_LOGLEVEL_ERROR,
772+
"Unable to add type1 font to requested PDF page");
773+
pdf_free(doc);
774+
return (1);
775+
}
759776

760777
#ifdef HAVE_OPEN_MEMSTREAM
761778
s = open_memstream(&buf, &len);
@@ -764,6 +781,7 @@ static int generate_banner_pdf(banner_t *banner,
764781
{
765782
if (log)
766783
log(ld, FILTER_LOGLEVEL_ERROR, "bannertopdf: Cannot create temp file: %s\n", strerror(errno));
784+
pdf_free(doc);
767785
return 1;
768786
}
769787
#endif
@@ -935,17 +953,21 @@ static int generate_banner_pdf(banner_t *banner,
935953
noptions,
936954
options);
937955

938-
/*
939-
* Try to find a PDF form in PDF template and fill it.
940-
*/
941-
int ret = pdf_fill_form(doc, known_opts);
956+
/*
957+
* Try to find a PDF form in PDF template and fill it.
958+
*/
942959

943-
/*
944-
* Could we fill a PDF form? If no, just add PDF stream.
945-
*/
946-
if (!ret)
960+
if (pdf_fill_form(doc, known_opts) != 0)
947961
{
948-
pdf_prepend_stream(doc, 1, buf, len);
962+
/*
963+
* Could we fill a PDF form? If no, just add PDF stream.
964+
*/
965+
966+
if (pdf_prepend_stream(doc, 1, buf, len) != 0)
967+
{
968+
if (log) log(ld, FILTER_LOGLEVEL_ERROR,
969+
"Unable to prepend stream to requested PDF page");
970+
}
949971
}
950972

951973
copies = get_int_option("number-up", noptions, options, 1);
@@ -954,7 +976,13 @@ static int generate_banner_pdf(banner_t *banner,
954976
copies *= 2;
955977

956978
if (copies > 1)
957-
pdf_duplicate_page(doc, 1, copies - 1);
979+
{
980+
if (pdf_duplicate_page(doc, 1, copies - 1) != 0)
981+
{
982+
if (log) log(ld, FILTER_LOGLEVEL_ERROR,
983+
"Unable to duplicate requested PDF page");
984+
}
985+
}
958986

959987
pdf_write(doc, outputfp);
960988

cupsfilters/pdf.cxx

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ extern "C" pdf_t * pdf_load_template(const char *filename)
6565
unsigned pages = (pdf->getAllPages()).size();
6666

6767
if (pages != 1) {
68-
fprintf(stderr, "ERROR: PDF template must contain exactly 1 page: %s\n",
69-
filename);
7068
delete pdf;
7169
return NULL;
7270
}
@@ -139,26 +137,21 @@ int pdf_pages_fp(FILE *file)
139137
* I - buffer containing data to be prepended
140138
* I - length of buffer
141139
*/
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)
146144
{
147145
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);
152148

153149
QPDFObjectHandle page = pages[page_num - 1];
154150

155151
// get page contents stream / array
156152
QPDFObjectHandle contents = page.getKey("/Contents");
157153
if (!contents.isStream() && !contents.isArray())
158-
{
159-
fprintf(stderr, "ERROR: Malformed PDF.\n");
160-
return;
161-
}
154+
return (1);
162155

163156
// prepare the new stream which is to be prepended
164157
PointerHolder<Buffer> stream_data = PointerHolder<Buffer>(new Buffer(len));
@@ -177,6 +170,8 @@ extern "C" void pdf_prepend_stream(pdf_t *pdf,
177170

178171
contents.insertItem(0, stream);
179172
page.replaceKey("/Contents", contents);
173+
174+
return (0);
180175
}
181176

182177

@@ -187,24 +182,19 @@ extern "C" void pdf_prepend_stream(pdf_t *pdf,
187182
* I - page number of the page to which the font is to be added
188183
* I - name of the font to be added
189184
*/
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)
193188
{
194189
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);
199192

200193
QPDFObjectHandle page = pages[page_num - 1];
201194

202195
QPDFObjectHandle resources = page.getKey("/Resources");
203196
if (!resources.isDictionary())
204-
{
205-
fprintf(stderr, "ERROR: Malformed PDF.\n");
206-
return;
207-
}
197+
return (1);
208198

209199
QPDFObjectHandle font = QPDFObjectHandle::newDictionary();
210200
font.replaceKey("/Type", QPDFObjectHandle::newName("/Font"));
@@ -218,14 +208,13 @@ extern "C" void pdf_add_type1_font(pdf_t *pdf,
218208
fonts = QPDFObjectHandle::newDictionary();
219209
}
220210
else if (!fonts.isDictionary())
221-
{
222-
fprintf(stderr, "ERROR: Can't recognize Font resource in PDF template.\n");
223-
return;
224-
}
211+
return (1);
225212

226213
font = pdf->makeIndirectObject(font);
227214
fonts.replaceKey("/bannertopdf-font", font);
228215
resources.replaceKey("/Font", fonts);
216+
217+
return (0);
229218
}
230219

231220

@@ -306,27 +295,23 @@ static void fit_rect(float oldrect[4],
306295
* I - Length of page to set
307296
* I - Scale of page to set
308297
*/
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)
314303
{
315304
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);
320307

321308
QPDFObjectHandle page = pages[page_num - 1];
322309
float new_mediabox[4] = { 0.0, 0.0, width, length };
323310
float old_mediabox[4];
324311
QPDFObjectHandle media_box;
325312

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);
330315

331316
fit_rect(old_mediabox, new_mediabox, scale);
332317
media_box = makeRealBox(new_mediabox);
@@ -336,6 +321,8 @@ extern "C" void pdf_resize_page (pdf_t *pdf,
336321
page.replaceKey("/CropBox", media_box);
337322
page.replaceKey("/MediaBox", media_box);
338323
page.replaceKey("/TrimBox", media_box);
324+
325+
return (0);
339326
}
340327

341328

@@ -345,22 +332,22 @@ extern "C" void pdf_resize_page (pdf_t *pdf,
345332
* I - page number of the page to be duplicated
346333
* I - number of copies to be duplicated
347334
*/
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)
351338
{
352339
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);
357342

358343
QPDFObjectHandle page = pages[page_num - 1];
359344
for (unsigned i = 0; i < count; ++i)
360345
{
361346
page = pdf->makeIndirectObject(page);
362347
pdf->addPage(page, false);
363348
}
349+
350+
return (0);
364351
}
365352

366353

@@ -408,7 +395,7 @@ std::string lookup_opt(opt_t *opt, std::string const& key) {
408395
* 3. Fill recognized fields with information.
409396
* I - Pointer to the QPDF structure
410397
* 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
412399
*/
413400
extern "C" int pdf_fill_form(pdf_t *doc, opt_t *opt)
414401
{
@@ -418,19 +405,15 @@ extern "C" int pdf_fill_form(pdf_t *doc, opt_t *opt)
418405
QPDFPageDocumentHelper pdh(*doc);
419406

420407
// 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;
425410

426411
// get the first page from the PDF to fill the form. Since this
427412
// is a banner file,it must contain only a single page, and that
428413
// check has already been performed in the `pdf_load_template()` function
429414
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;
434417
QPDFPageObjectHelper page = pages.front();
435418

436419
// get the annotations in the page
@@ -467,7 +450,7 @@ extern "C" int pdf_fill_form(pdf_t *doc, opt_t *opt)
467450
}
468451
}
469452

470-
// status 1 notifies that the function successfully filled all the
453+
// status 0 notifies that the function successfully filled all the
471454
// identifiable fields in the form
472-
return 1;
455+
return 0;
473456
}

cupsfilters/pdf.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ struct _opt {
4040
pdf_t * pdf_load_template(const char *filename);
4141
void pdf_free(pdf_t *pdf);
4242
void pdf_write(pdf_t *doc, FILE *file);
43-
void pdf_prepend_stream(pdf_t *doc, unsigned page, char const *buf, size_t len);
44-
void pdf_add_type1_font(pdf_t *doc, unsigned page, const char *name);
45-
void pdf_resize_page(pdf_t *doc, unsigned page, float width, float length, float *scale);
46-
void pdf_duplicate_page (pdf_t *doc, unsigned page, unsigned count);
43+
int pdf_prepend_stream(pdf_t *doc, unsigned page, char const *buf, size_t len);
44+
int pdf_add_type1_font(pdf_t *doc, unsigned page, const char *name);
45+
int pdf_resize_page(pdf_t *doc, unsigned page, float width, float length, float *scale);
46+
int pdf_duplicate_page (pdf_t *doc, unsigned page, unsigned count);
4747
int pdf_fill_form(pdf_t *doc, opt_t *opt);
4848
int pdf_pages(const char *filename);
4949
int pdf_pages_fp(FILE *file);

0 commit comments

Comments
 (0)