0

I get the following stdout error when attemping to convert a PDF to a PNG. Anyone have an idea what is wrong with the PDF file?

This problem only affects certain PDF files and this particular PDF was actually split out of a larger PDF, resized & compressed by GhostScript.

The command runs, does not return an error but the stdout has the following warnings and the output PNG file exists but has dimensions of 0 and 0.

stdout:

  **** Error reading a content stream. The page may be incomplete.
               Output may be incorrect.
Error: /typecheck in --run--
Operand stack:
   --dict:3/3(L)--   4   9463495   4   0   --nostringval--   MediaBox   --nostringval--   Parent   --nostringval--   Type   Page   Contents   --nostringval--   Resources   --nostringval--   ColorSpace   --nostringval--   ExtGState   --nostringval--   Font   --nostringval--   ProcSet   --nostringval--   XObject   --nostringval--   R1000   --nostringval--   R10000   --nostringval--   R10001   --nostringval--   R10002   --nostringval--   R10003   --nostringval--   R10004   --nostringval--   R10005   --nostringval--   R10006   --nostringval--   R10007   --nostringval--   R10008   --nostringval--   R10009   --nostringval--   R1001   --nostringval--   R10010   --nostringval--   R10011   --nostringval--   R10012   --nostringval--   R10013   --nostringval--   R10014   --nostringval--   R10015   --nostringval--   R10016   --nostringval--   R10017   --nostringval--   R10018   --nostringval--   R10019   --nostringval--   R1002   --nostringval--   R10020   --nostringval--   R10021   --nostringval--   R10022   --nostringval--   R10023   --nostringval--   R10024   --nostringval--   R10025   --nostringval--   R10026   --nostringval--   R10027   --nostringval--   R10028   --nostringval--   R10029   --nostringval--   R1003   --nostringval--   R10030   --nostringval--   R10031   --nostringval--   R10032   --nostringval--   R10033   --nostringval--   R10034   --nostringval--   R10035   --nostringval--   R10036   --nostringval--   R10037   --nostringval--   R10038   --nostringval--   R10039   --nostringval--   R1004   --nostringval--   R10040   --nostringval--   R10041   --nostringval--   R10042   --nostringval--   R10043   --nostringval--   R10044   --nostringval--   R10045   --nostringval--   R10046   --nostringval--   R10047   --nostringval--   R10048   --nostringval--   R10049   --nostringval--   R1005   --nostringval--   R10050   --nostringval--   R10051   --nostringval--   R10052   --nostringval--   R10053   --nostringval--   R10054   --nostringval--   R10055   --nostringval--   R10056   --nostringval--   R10057   --nostringval--   R10058   --nostringval--   R10059   --nostringval--   R1006   --nostringval--   R10060   --nostringval--   R10061   --nostringval--   R10062   --nostringval--   R10063   --nostringval--   R10064   --nostringval--   R10065   --nostringval--   R10066   --nostringval--   R10067   --nostringval--   R10068   --nostringval--   R10069   --nostringval--   R1007   --nostringval--   R10070   --nostringval--   R10071   --nostringval--   R10072   --nostringval--   R10073   --nostringval--   R10074   --nostringval--   R10075   --nostringval--   R10076   --nostringval--   R10077   --nostringval--   R10078   --nostringval--   R10079   --nostringval--   R1008   --nostringval--   R10080   --nostringval--   R10081   --nostringval--   R10082   --nostringval--   R10083   --nostringval--   R10084   --nostringval--   R10085   --nostringval--   R10086   --nostringval--   R10087   --nostringval--   R10088   --nostringval--   R10089   --nostringval--   R1009   --nostringval--   R10090   --nostringval--   R10091   --nostringval--   R10092   --nostringval--   R10093   --nostringval--   R10094   --nostringval--   R10095   --nostringval--   R10096   --nostringval--   R10097   --nostringval--   R10098   --nostringval--   R10099   --nostringval--   R1010   --nostringval--   R10100   --nostringval--   R10101   --nostringval--   R10102   --nostringval--   R10103   --nostringval--   R10104   --nostringval--   R10105   --nostringval--   R10106   --nostringval--   R10107   --nostringval--   R10108   --nostringval--   R10109   --nostringval--   R1011   --nostringval--   R10110   --nostringval--   R10111   --nostringval--   R10112   --nostringval--   R10113   --nostringval--   R10114   --nostringval--   R10115   --nostringval--   R10116   --nostringval--   R10117   --nostringval--   R10118   --nostringval--   R10119   --nostringval--   R1012   --nostringval--   R10120   --nostringval--   R10121   --nostringval--   R10122   --nostringval--   R10123   --nostringval--   R10124   --nostringval--   R10125   --nostringval--   R10126   --nostringval--   R10127   --nostringval--   R10128   --nostringval--   R10129   --nostringval--   R1013   --nostringval--   R10130   --nostringval--   R10131   --nostringval--   R10132   --nostringval--   R10133   --nostringval--   R10134   --nostringval--   R10135   --nostringval--   R10136   --nostringval--   R10137   --nostringval--   R10138   --nostringval--   R10139   --nostringval--   R1014   --nostringval--   R10140   --nostringval--   R10141   --nostringval--   R10142   --nostringval--   R10143   --nostringval--   R10144   --nostringval--   R10145   --nostringval--   R10146  

Here are the details of the original PDF (before splitting/resizing):

Content Creator: Bluebeam Revu x64

Encoding Software: macOS Version 10.14.6 (Build 18G95) Quartz PDFContext

Code (Node.js):

exports.convert = function(pdfPath){
    return new Promise( (resolve, reject) => {
        //Convert pdf
        console.log('pdf2Image: Version', 0.42);

        //Remove extension
        let path = pdfPath.split('.')[0];
        let splitPathTest = path.split('/');
        let outputFileName = splitPathTest[splitPathTest.length - 1] + '.png';
        let outputDirectory = splitPathTest.slice(0, splitPathTest.length - 1).join('/') + '/';
        let outputPath = outputDirectory + outputFileName;

        console.log('pdfPath', pdfPath);
        console.log('outputPath', outputPath);
        console.log('output Filename ', outputFileName);

        if (fs.existsSync(pdfPath)) {
            console.log('pdfPath exists. PDF found.');
        }else{
            console.warn('pdfPath does not exist. PDF not found.');
        }


        //============== Ghostscript
        //Conver PDF -> PNG
        console.log('gs - starting');
        gs()
            .batch()
            .nopause()
            .option('-r' + 50 * 2)
            .option('-dDownScaleFactor=2')
            .executablePath('lambda-ghostscript/bin/./gs')
            .device('png16m')
            .output(outputPath)
            .input(pdfPath)
            .exec(function (err, stdout, stderr) {
                if (!err) {
                    console.log('gs executed w/o error');            
                    console.log('stdout',stdout);            
                    console.log('stderr',stderr);
                    console.log('output saved to: ', outputPath);
                    resolve(outputPath);
                } else {
                    console.log('gs error:', err);
                    reject();
                }
            });
    })
}
Badrush
  • 1,247
  • 1
  • 17
  • 35

1 Answers1

1

The error is pretty self-explanatory really, the PDF file is broken in some way. Ghostscript tried to recover and was unable to do so, so it emitted an error and stopped (which is why your PNG is empty). Note that's not a warning, its an error.

The error is a typecheck; so Ghostscript encountered an object whose type was inappropriate for the operation. For example, a string operand to an arithmetic operation, or a number where a string was expected etc.

Without seeing the file I cannto say more than that. You haven't said what the version of Ghostscript is, and I have no idea what 'lmbda-ghostscript' is, but it doesn't sound like its the canonical release. If I were you the first thing I would do is get hold of the Ghostscript source and build it, then try that from the shell. If that fails then you can open a bug report and someone can tell you why.

Ah, apparently 'lmbda-ghostscript' is some kind of AWS thing. Not only that, its very old. The most recent version appears to be 9.20 which is three years old and has some well-known and publicised security vulnerabilities. I'd still suggest that you try the entire process form beginning to end locally using the current version (9.50), if that works then you really need to figure out how to use it. If not then you can report a bug.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Thank you for your insight. I was able to resolve the issue by changing these 3 params when resizing the pdf the first time before converting to PNG: `-dCompatibilityLevel=1.3` `-dEmbedAllFonts=true` `-dPDFSETTINGS=/ebook` – Badrush Nov 18 '19 at 19:00
  • If you're going to render it to a PNG, why create a new PDF file anyway ? The PDFSETTINGS switch changes **way** more than 1 thing, that alters many of the configuration parameters all at once. These are documented in https://www.ghostscript.com/doc/9.50/VectorDevices.htm#PDFWRITE – KenS Nov 18 '19 at 19:24
  • I actually referenced that table before deciding to try. I resize the PDFs because I need to 1) Compress the PDF 2)Resize it to the required dimensions and then separately, as needed I sometimes need to convert them into an image for a different use case. – Badrush Nov 18 '19 at 19:28
  • is there a tool that can be used on a Mac to inspect a PDF file for errors? – Badrush Nov 19 '19 at 15:00
  • 1
    Adobe Acrobat Pro (Preflight tool), VeraPDF (dedicated PDF conformance tool), Ghostscript will tell you if there are problems with a PDF file (and try to repair or ignore them) No doubt there are others. – KenS Nov 19 '19 at 16:18