]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
pdftops: added handling of empty input 196/head
authorJaiLuthra1 <luthrajaiji@gmail.com>
Tue, 28 Jan 2020 18:28:32 +0000 (23:58 +0530)
committerJaiLuthra1 <luthrajaiji@gmail.com>
Tue, 28 Jan 2020 18:28:32 +0000 (23:58 +0530)
Makefile.am
filter/pdf.cxx
filter/pdf.h
filter/pdftops.c

index 43f7a54e8d5d0a554d44db178e0bf59d37cfe336..cc623cc68afd53cb002688af22de1bf0297c65ec 100644 (file)
@@ -789,10 +789,17 @@ texttotext_DEPENDENCIES = $(STRCASESTR)
 pdftops_SOURCES = \
        filter/common.c \
        filter/common.h \
-       filter/pdftops.c
+       filter/pdftops.c \
+       filter/pdf.cxx \
+       filter/pdf.h
 EXTRA_pdftops_SOURCES = filter/strcasestr.c
-pdftops_CFLAGS = $(CUPS_CFLAGS)
-pdftops_LDADD = $(STRCASESTR) $(CUPS_LIBS)
+pdftops_CFLAGS = \
+       $(CUPS_CFLAGS) \
+       $(LIBQPDF_CFLAGS)
+pdftops_LDADD = \
+       $(STRCASESTR) \
+       $(CUPS_LIBS) \
+       $(LIBQPDF_LIBS)
 pdftops_DEPENDENCIES = $(STRCASESTR)
 
 pdftoraster_SOURCES = \
index f12607f860b112cc9b95664ec3272fc1848d2efa..175df21510c039f6c6b48e2480fda0a31abd3c00 100644 (file)
@@ -84,6 +84,24 @@ extern "C" void pdf_free(pdf_t *pdf)
   delete pdf;
 }
 
+/*
+ * 'pdf_pages()' - Count number of pages in file
+ *                         using QPDF.
+ * I - Filename to open
+ * O - Number of pages or -1 on error
+ */
+int pdf_pages(const char *filename)
+{
+  QPDF *pdf = new QPDF();
+  try{
+  pdf->processFile(filename);
+  }catch(...) {
+    return -1;
+  }
+  int pages = (pdf->getAllPages()).size();
+  return pages;
+}
+
 
 /**
  * 'pdf_prepend_stream' - Prepend a stream to the contents of a specified
index 18dcfa90b31b7510d6595b930fcf4df168d3bec1..eaaf1903fee323686a8b751871ca6d2481f68ff1 100644 (file)
@@ -45,6 +45,7 @@ void pdf_add_type1_font(pdf_t *doc, unsigned page, const char *name);
 void pdf_resize_page(pdf_t *doc, unsigned page, float width, float length, float *scale);
 void pdf_duplicate_page (pdf_t *doc, unsigned page, unsigned count);
 int pdf_fill_form(pdf_t *doc, opt_t *opt);
+int pdf_pages(const char *filename);
 
 #ifdef __cplusplus
 }
index 2ff74c6c412b7dcf71ef05a7b55cc8b81c331945..8444e81f568753a7ac09ee50ac9b73a7e18d9a39 100644 (file)
@@ -23,6 +23,7 @@
  * Include necessary headers...
  */
 
+#include "pdf.h"
 #include <config.h>
 #include <cups/cups.h>
 #include <cups/ppd.h>
@@ -224,6 +225,42 @@ void remove_options(char *options_str, const char **option_list)
 }
 
 
+/*
+ * Check whether given file is empty
+ */
+
+int is_empty(char *filename)
+{
+  FILE *fp = NULL;
+  fp = fopen(filename, "rb");
+  if (fp == NULL)
+  {
+    fprintf(stderr, "ERROR: pdftops - cannot open print file \"%s\"\n",
+            filename);
+    exit(1);
+  }
+  else
+  {
+    char buf[1];
+    rewind(fp);
+    if (fread(buf, 1, 1, fp) == 0) {
+      fclose(fp);
+      fprintf(stderr, "DEBUG: Input is empty, outputting empty file.\n");
+      return 1;
+    }
+    fclose(fp);
+    int pages = pdf_pages(filename);
+    if (pages == 0) {
+      fprintf(stderr, "DEBUG: No pages left, outputting empty file.\n");
+      return 1;
+    }
+    if (pages > 0)
+      return 0;
+    exit(1);
+  }
+}
+
+
 /*
  * Before calling any command line utility, log its command line in CUPS'
  * debug mode
@@ -381,6 +418,9 @@ main(int  argc,                             /* I - Number of command-line args */
     tempfile[0] = '\0';
   }
 
+  if (is_empty(filename))
+    return 0;
+
  /*
   * Read out copy counts and collate setting passed over by pdftopdf
   */