Read latest Technology News & never miss out

Trick for FPDI free pdf parser that supports PDF version above 1.4

One of our website visitor asked a question –

Is there any free/open-source for FPDI free Pdf parser to make it compatible with PDF version higher than 1.4 ?

Starting from the introduction of FDPI , it’s a collection of PHP classes allowing developers to read pages from PDF files & use them as templates in FPDF package, which was developed by Olivier Plathey. Apart from a copy of FPDF, FPDI doesn’t require any special PHP extensions. For encrypting PDF files FDPI with FDPF and FDPI-protection package is required.

Directly coming to the point , By default FPDI pdf-parser can “only” handle PDF documents up to PDF version 1.4. With PDF version 1.5 there were new compression features introduced which involve changes in internal structure of a PDF document. Commercial version of FPDI PDF-parser supporting PDF document above 1.4 is available at Setasign but as said its commercial version and ask for payment.

There are many developers who may want to Encrypt PDF (version above 1.4) from free version of FPDI PDF parser but FPDI free PDF parser doesn’t support it and display one of the following error messages like –

PHP Fatal error: Uncaught exception 'Exception' with message 'This document (/tmp/document.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)' in /FPDI-1.5.2/pdf_parser.php:329

Indeed getting a code trick and making a free tool compatible for our premium need is much better idea than buying a commercial version of tool. Think like a real smart developer and you will get the code idea of what you want to get done.

Indeed , the simplest way is to convert the PDF file version before it is submitted to FDPI pdf parser. we need to first check the PDF version of file with a PHP code.

PDF version of file is mostly present in first line of PDF. so we need to read the first line of PDF which will give us something like below response stored in a variable $line_first when using above PHP code –

%PDF-1.5%

Here we get the PDF version but we need to separate version number(1.5) from complete line %PDF-1.5%. so extracting the number from variable $line_first and storing the numeric value in variable $pdfversion using below code.

// extract number such as 1.4 ,1.5 from first read line of pdf filepreg_match_all('!\d+!', $line_first, $matches); // save that number in a variable$pdfversion = implode('.', $matches[0]);

we need to now compare the PDF version of submitted file from 1.4 in such away that if value of variable $pdfversion is greater than 1.4 than proceed with if condition otherwise proceed with else condition.

Here we get the PDF version in “only” numeric value stored in a variable/string , as we know very well that free PDF parser only support PDF file upto version 1.4 , so now its time to implement the main logic of converting our PDF file(version above 1.4) into PDF version 1.4 using GHOSTSCRIPT command line. Please make sure that GHOSTSCRIPT is already installed on your server.

use the below command line GHOSTSCRIPT code under PHP exec()

// USE GHOSTSCRIPT IF PDF VERSION ABOVE 1.4 AND SAVE ANY PDF TO VERSION 1.4 , SAVE NEW PDF OF 1.4 VERSION TO NEW LOCATION <?phpexec('gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="'.$srcfile_new.'" "'.$srcfile.'"'); ?>

// extract number such as 1.4 ,1.5 from first read line of pdf file preg_match_all('!\d+!', $line_first, $matches);

// save that number in a variable $pdfversion = implode('.', $matches[0]);

// compare that number from 1.4(if greater than proceed with ghostscript) if($pdfversion > "1.4"){ // USE GHOSTSCRIPT IF PDF VERSION ABOVE 1.4 AND SAVE ANY PDF TO VERSION 1.4 , SAVE NEW PDF OF 1.4 VERSION TO NEW LOCATION shell_exec('gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="'.$srcfile_new.'" "'.$srcfile.'"');

From below You can generate the download link for complete zip for Encrypting any version of PDF files (Includes FPDI + FPDI-protection + FDPF). After generating the link, it will expire in 1 hour. Once expired, you need to re-generate it again in order to download zip.

Generate Download link

Subscribe right now for new blogs notifications and always stay connected with us. please click here

Looking for a better dedicated resource ?

Join Our Newsletter

Stay updated with latest changes in technologies and get better business advice from us.

Your Name

Your email

Subscribe

ABOUT US

We at Webnius Infotech, believe in Client Satisfaction and Professional Quality of Secured Work exactly according to Google recommendations. With Encrypted and Quality work techniques, WEBNIUS sets the benchmark when it comes to Developing and Designing of Secured Websites and high-end web applications.