2

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.

FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI

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?

Community
  • 1
  • 1
haakym
  • 12,050
  • 12
  • 70
  • 98
  • What do you mean by "added it to my path"? Thanks in advance. – Marsha Dec 06 '16 at 04:12
  • @Marsha see my answer - I updated it with the relevant info, let me know if it doesn't make sense. – haakym Dec 06 '16 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 Dec 06 '16 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 Jan 18 '17 at 02:05
  • @Marsha Awesome!!!! Well done and happy coding! – haakym Jan 18 '17 at 09:01

2 Answers2

7

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:

  1. gswin32c.exe
  2. gswin32.exe

The 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:

  1. My Computer
  2. Properties
  3. Advanced system settings
  4. Environment variables
  5. 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 windows key + 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!

Community
  • 1
  • 1
haakym
  • 12,050
  • 12
  • 70
  • 98
  • I am looking around, but are you aware of a php wrapper class for ghostscript interaction? – Shmack Jan 29 '21 at 16:57
  • @ShanerM13 does this help? https://github.com/xthiago/pdf-version-converter – haakym Jan 29 '21 at 20:14
  • thats a php pdf version converter? I don't see it supporting ps to pdf :( – Shmack Jan 29 '21 at 20:42
  • Oh sorry! I'd quickly read your message on my phone and digging way back into my memory I thought that had gs wrapped up in it but just realised it is a requirement. Any reason you can't just use ghostscript directly, do you not have access? – haakym Jan 29 '21 at 20:50
  • Yah, thats the problem. And the box is hosted by a 3rd party... they cant remove the restrictions on my machine. Otherwise Id just use `exec` – Shmack Jan 29 '21 at 21:06
  • Ah gotcha. I think you're best making your own question here on SO. Sorry I can't help! – haakym Jan 30 '21 at 10:04
1

For 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 ');
lisandro
  • 454
  • 4
  • 12
  • Thank you sir. Same issue for me. I had set in environment variable but it is not taking. When I have added full path in command then it's works. – Bhavin Thummar Jan 07 '21 at 05:09