Using Laravel for a web app where I am using https://github.com/clegginabox/pdf-merger to merge pdf files and ran into the following error when trying to merge two pdfs together where one was over version 1.4:
Exception in pdf_parser.php line 133:
This document (C:\path-to-doc\file.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 pdf_parser.php line 133
I followed the suggestion in the following answer to use ghostscript to convert any pdf over 1.4 to 1.4 so the merge will work.
I've installed ghostscript successfully and added it to my path. For testing I have a sample pdf file which is version 1.5 called test.pdf and I run the following command from the windows terminal:
gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf
and I get my new-file.pdf just fine as version 1.4.
Now I have the following php script which when ran from the web browser just loads continuously. If I let it run a bit sometimes a new file has been created but it's like 3kb in size and blank and also the original pdf file comes blank sometimes?!
<?php
shell_exec( "gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf");
echo 'done';
Any ideas what I'm doing wrong?
-
What do you mean by "added it to my path"? Thanks in advance.Marsha– Marsha2016年12月06日 04:12:56 +00:00Commented Dec 6, 2016 at 4:12
-
@Marsha see my answer - I updated it with the relevant info, let me know if it doesn't make sense.haakym– haakym2016年12月06日 11:37:55 +00:00Commented Dec 6, 2016 at 11:37
-
@Marsha if you're trying to use the package in my question or trying to merge pdfs let me know as I have had a bit more experience with this now and may be able to give some tips.haakym– haakym2016年12月06日 11:39:06 +00:00Commented Dec 6, 2016 at 11:39
-
thank you so much for the detail!! It really helps me because I've never had experience in using terminal before. And thank you for the offer of the helps, but I've successfully implemented it and it works :)Marsha– Marsha2017年01月18日 02:05:36 +00:00Commented Jan 18, 2017 at 2:05
-
@Marsha Awesome!!!! Well done and happy coding!haakym– haakym2017年01月18日 09:01:38 +00:00Commented Jan 18, 2017 at 9:01
3 Answers 3
Was just about to post this question and figured it out, so went ahead and answered for the benefit of others.
I read this answer on super user https://superuser.com/questions/200188/reading-a-pdf-file-for-testing-in-ghostscript#answer-200784
This part being the light bulb
If you use Ghostscript on Windows, you'll have two executables:
gswin32c.exegswin32.exeThe first one is to be run from inside a 'DOS box' (i.e. cmd.exe window) -- either interactively or not. It prints all stderr/stdout messages into the cmd.exe window and also expects any input commands to be typed in there.
The second one opens a separate Window for "interactivity': prints stderr/stdout to separate window, and expects commands there.
So I changed my php script to use gswin64c and to output the text from the command's response:
$output = shell_exec( "gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test.pdf");
echo "<pre>$output</pre>";
This showed that I was referencing the wrong file name i.e. test-file.pdf instead of test.pdf.
Error: /undefinedfilename in (test-file.pdf)
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
--dict:1192/1684(ro)(G)-- --dict:0/20(G)-- --dict:78/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
After updating the file name it all worked just fine!
Update
In response to Marsha's comment in the question...
What do you mean by "added it to my path"? Thanks in advance.
When you add an executable or the location to a group of executables to your PATH environment variable it allows executable(s) to be invoked from the command line from any location. For example you could invoke ghostscript from C:\youruser\Desktop after adding it to your path, instead of being in the folder where your ghostscript files reside C:\gs\bin or whatever it is.
Sometimes the software you're installing will do this for you automatically and sometimes you need to do it manually yourself (as is the case with ghostscript).
How to add an executable to the PATH environment variable:
- My Computer
- Properties
- Advanced system settings
- Environment variables
- Then append the executable to the end of the existing value in either user variables/PATH or system variables/Path - if you use user variables it will only apply to your user if you use system variables it will apply to all users
Tips:
- You can skip to step 3 if you press
windowskey +pause - You must separate new entries in the path variable with a semi-colon
;
If you google something like "add exe to my path windows" I'm sure you'll find plenty of info on this. Hope this helped!
6 Comments
execFor some reason Apache is not getting the windows PATH environmental variable, the solution I've found was to write the complete path to the gs executable between double quotes if it has spaces in it .
exec('"C:\Program Files\gs\gs9.27\bin\gs.exe" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=test.png test.pdf ');
2 Comments
Add comments to PDF file.
exec('"gswin32c.exe" -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPreserveAnnots=false -dAutoRotatePages=/None -sFONTPATH="C:/WINDOWS/Fonts" -sOutputFile="'.$outputFile.'" -sPDFPassword="'.$inputFilePassword.'" -f "'.$inputFile.'"')