I needed to parse a PDF file to images using php. I've done it with the help of ghostscript. Here is the script:
$result = exec("gs -sDEVICE=jpeg -sOutputFile=%03d.jpeg $pdfname.pdf");
But I need the final images to be in another folder. I've tried writing -sOutputFile=/img/%03d or -sOutputFile=img/%03d and in a lot of similar ways...but the program doesn't even work when I add these things.
You need to create the /img/ or ./img directories before you run Ghostscript.
Ghostscript will not automatically create them if they don't exist.
Related
I am using laravel framework version 5.1. My requirement is to upload multiple files of types doc, docx, pdf and to save this file as a single PDF file. I have found packages that can combine/edit PDF files. But is there any library/package available for laravel to do this with doc/docx files too?
I found that LibreOffice allows command line conversion using the LibreOffice conversion engine (which DID preserve the formatting like I wanted and generally worked great).
libreoffice --headless -convert-to pdf fileToConvert.docx -outdir output/path/for/pdf
But I didn't have admin rights on my host's web server.
Then I ran across a cool project made by a Ph.D. student (Philip J. Guo) at Stanford called CDE(Code Data Environment). In short, it allows one to avoid dependency hell by copying all the files used when you run certain commands, recreating the linux environment where the command worked. I was able to use this to run LibreOffice without having to resort to someone's portable version of it, and it worked just like it did when I did it on Ubuntu with the command above, with a tweak: I needed to run the wrapper of LibreOffice the CDE generated.
I don't know of a catch all package to do this but as i can not comment yet (just joined), here is how i would do it.
upload the files using using your form and controller do your validation, storage etc.
Then pass an array of the paths to the files you have uploaded to a blade template and embed them on the page depending on the file type.
This package
https://github.com/barryvdh/laravel-dompdf
Can convert a html template to a pdf.
Hope it points you in the right direction
$pdf = App::make('dompdf');
$files = ['your file paths'];
$pdf->loadView('template.path', ['files' => $files])->setPaper('A4')->setOrientation('portrait');
return $pdf->stream('download.pdf');
I'm using Filepicker.io to upload PDFs to my application. I have all those URLs and now I am trying to merge some of those PDFs using the PDF Tool Kit PHP library. It was not working for me so I ran some tests using the "file_exists" on PHP and it kept returning false.
I think this has to do with the fact that the URL does not have a ".pdf" extension at the end. This is what they look like: "https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1"
Does anyone know how I can pull the PDF using PHP in order to merge those files using the PDF Toolkit Library?
Thanks!
Alain F.
file_exists doesn't work with URLs, only with local files. Instead download the file to the temp dir using the copy command.
If the file can't be downloaded, the copy command will return false.
$exists = copy('https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1', '/tmp/example.pdf');
if (!$exists) throw new Exception("PDF could not be downloaded");
Use the downloaded file in the PDF Tool Kit.
EDIT: This does not solve this particular problem but does address the theory that it didn't work because "the URL does not have a ".pdf" extension at the end."
You can add things to the end of the filepicker URL with a trailing +
The following urls are equivalent:
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1+name.pdf
imagemagick convert does not work through php shell_exec but does through a shell.
In a shell convert file.pdf file.png works fine. But when I have this within a php file as shell_exec('convert file.pdf file.png'); Then I get no output! I have the permissions to do this, so I think it isn't that that's the problem; I have checked the directory I am in by way of getcwd() and this is also ok.
I know shell_exec works because I have used it earlier in the code and that works fine.
Any ideas?
I got the solution thanks to Crontab from another thread. I quote from there:
[I]f you're trying to convert a PDF to a PNG using ImageMagick ...
even using the full path to ImageMagick's convert won't work if the
script's PATH doesn't contain the path location to Ghostscript also.
Without messing with any user paths, you could add:
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Or something similar depending on your setup. The gs executable has
to be in your script user's path somewhere or ImageMagick will fail to
convert PDF or EPS files.
Try the full path to convert, i.e. shell_exec('/usr/bin/convert file.pdf file.png);. You can use which convert to find the location on your system.
There are several reason why this could happen, but I suggest reading this page and the user comments:
http://php.net/manual/en/function.shell-exec.php
I am trying to process an externally hosted image using a php command.
Here is the code:
exec('convert -resize "568x150" http://www.temp.com/temp.jpg scripts/newtemp.jpg');
If I run this command in it's own file called for example test.php in my wordpress directory then it works fine, resizes the image and saves it into the /scripts folder as newtemp.jpg
The problem occurs when I put this command into my single.php, then the codes doesn't seem to work and the image is not saved.
Is there an obvious reason this wont work within the single.php?
Convert is ImageMagick, right? You could just use its native php extension and don't have to hassle with exec-ing it yourself.
I'm using pdftk for flattening PDF files on server. For this purpose I use PHP.
This is my code in PHP. Btw, I'm using this on WAMP.
passthru("pdftk editablepdf/jason.pdf output flattenpdf/flattened.pdf flatten");
The pdftk.exe file and the accompanying .dll file both exist in the www directory.
editablepdf and flattenpdf are two separate directories in the www directory.
After executing the script and checking, i find the pdfs haven't been saved.
When i tried running pdftk via command prompt, it worked fine. But the same is not happening here. Is it something that's gotta do with passthru??
Thanks and Regards
Sameer
was able to solve after looking around.
Here's the working line -
passthru("pdftk editablepdf\\jason.pdf output flattenpdf\\flattened.pdf flatten");
Since \ acts as escape sequence character, i'm using "\ \" to denote the folder levels.