9

Is there a free/open-source PDF parser out there that can do the job? The free parser that comes with FPDI only supports PDF version up to 1.4. I tried TCPDF but it didn't work.

I know I can change the PDF version of a PDF file thru Acrobat but I had experienced bug on doing this.

I am using FPDI to watermark PDFs we're selling on our company. I noticed one of the PDF I downgraded to 1.4 from 1.7 thru Acrobat looks pretty much the same but after my watermark function is done, the PDF will have white spaces on the right and bottom part of my cover page which has a black background. In short, the PDF looked bad to sell after the whole process.

kevinandrada
  • 449
  • 2
  • 5
  • 14

4 Answers4

8

I hit this same limitation in a project I'm currently working on, and ended up creating my own parser based on TCPDF's parser which works with a modified verision of FPDI called TCPDI and an unmodified copy of FPDF_TPL. It works with TCPDF 6, and supports up to at least PDF 1.6 (I haven't got a 1.7 PDF handy to try, but I'll be hunting one down shortly to make sure it works).

If you're still wanting to do this, please feel free to try out TCPDI / tcpdi_parser - if you encounter any issues, please report them via Github and I'll look into them. Basic installation and usage instructions can be found in the TCPDI README.

NullColaShip
  • 368
  • 3
  • 12
  • Nice work ! Can it work only using FPDF ? I created all my PDF with FDPF and I don't want to revisite all my functions to fit to TCPDF. Thanks. – Vincent Decaux Mar 25 '14 at 09:25
  • 1
    Does your parser (or tcpdi) handle protecting PDF files with a password and only allowing certain permissions? I didn't see that in your code, so I wasn't sure. – mts7 Jan 12 '16 at 22:30
5

I've worked around this limitation by using pdftk to uncompress a PDF before loading it into FPDI, then re-compressing it with pdftk afterwards.

I did look into a paid license for FPDI but struggled immensly trying to compile and get the evaluation version running and lost hope/confidence. TCPDI lacked any real installation path other than forking or cloning and they all seemed patchy at best on PHP 7.4.

The solution looks a little like this once you've installed pdftk on your machine:

$PDF = new Fpdi();
try {
    $PDF->setSourceFile('./pdf.pdf');
} catch (\Exception $exception) {
    if (aBoolFunctionToDetectThisParticularException($exception)) {
        exec('pdftk ./pdf.pdf output ./pdf_expanded.pdf uncompress');
        $PDF->setSourceFile('./pdf_expanded.pdf');
    } else {
        throw $exception;
    }
}

If you've gone down this route, it's a good idea to recompress as the difference in filesize is dramatic.

exec('pdftk ./pdf_expanded.pdf output ./pdf_compressed.pdf compress');
OK sure
  • 2,617
  • 16
  • 29
  • The same works with https://github.com/qpdf/qpdf ```exec('qpdf --stream-data=uncompress '.$originalPath.' '.$compressedPath);``` and later ```exec('qpdf --stream-data=compress '.$compressedPath . ' ' . $originalPath);``` – Pawel Apr 13 '22 at 14:58
  • I forgot to mention in QPDF you need to use also this flag `--force-version=1.4` – Pawel Apr 21 '22 at 10:21
3

FPDI allows processing PDF files up to version 1.4. You can use GHOSTSCRIPT to convert any PDF file to version 1.4 before processing it with FPDI.

Example Code is available on my official website blog this link.

UPDATE -

Please follow this link now. The domain name has been changed from webnius.com to infoconic.com

  • That link's website is gone, so here's the wayback machine archive of it: https://web.archive.org/web/20190415124533/http://www.webnius.com/blog/trick-for-fpdi-pdf-parser-that-supports-pdf-version-above-1-4/ – ssfinney Oct 18 '19 at 14:18
  • thanks for the full practical example in the link, it would be great if you produce a generic solution and add to this answer here. – Rakibul Haq Nov 26 '19 at 07:00
-3

Because of the high google result when you search on FPDI. The paid version can do above 1.4:

https://www.setasign.com/products/fpdi-pdf-parser/details/

When you love the software buy it, it ain't allot and you help the developer. :)

Patrick Rennings
  • 191
  • 1
  • 5
  • 19