how to convert PHP GD generated images into PDF - php

our website has a certification image created by PHP GD, and I can print and watch it on a web page. But I couldn't make it a downloadable PDF file. Is there a way I can do that?

You can first save the image somewhere and then use the FPDF lib to write it in a PDF file

you could use imagick to do this..
the below command does the trick if your server is properly configured - Imagick is installed, GhostScript is installed, php is allowed to execute shell commands, etc
shell_exec('convert /path/to/input.png /path/to/output.pdf')

Related

How can I add tags to a pdf document using PHP?

I wonder how I can add tags to a PDF document using PHP. The idea is that I can search for a document using the tag that I assigned it previously.
Your hint is exiftool to manipulate PDF tags. There is a PHP wrapper/driver for it. BIG NOTE that it is not recommended for production.
You have to install pdf to image converter on your server, here is the settings:
Please update the server with this package, so the conversion functionality works.
you can found the files i used on local server from
i used on windows using follwing instructions
Install gs909w32.exe (http://downloads.ghostscript.com/public/gs909w32.exe )
Install ImageMagick-6.8.9-4-Q16-x64-dll.exe (Link )
paste "php_imagick.dll" file in C:\wamp\bin\php\php5.4.3\ext\
include extensions= php_imagick.dll in php.ini file
It will convert your .pdf document to images (1 image/1 page), then you can tag....

How to convert a pdf to jpeg using ImageMagick and php

I'm having a hard time converting a pdf file to jpg using the php Imagick Library on a wordpress based web site.
I have done some reading and I think I need to install Ghostscript to achieve my goal.
Is that the case or I can do it without Ghostscript.
My code looks like that:
$img = new Imagick();
$img->setResolution(200,350);
$img->readImageFile($link.'[0]');
$img->setImageFormat('jpeg');
$img->destroy();
$img->clear();
I can successfully run the above code without the setImageFormat. setImageFormat produces an error. All other commands run successfully though.
Any ideas??
Thanks
You'll need to install GhostScript. PDF is handled by the GhostScript interpreter.

PHP - Grab PDF with URL that does not have the .pdf extension

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

Why can imagemagick save out image previews but fpdf not save out pdf files to server side folders

I tried the sample codes for imagemagick and it was able to save out document previews to the folder on the server.
I tried sample code for fpdf and it was able to render the PDF file fine when it is sent but fails when I ask it to save for directory.
Researching online it says the resolution to the fpdf issue is to set file permission to 777 (any source access permitted, as I understand). My default folder permission is 755.
I want to ask if anyone knows why I can save with imagemagick but not with fpdf.
The reason, I suspect, is because imagemagick calls requires exec() which is effectively command line access. As I understand it however, fpdf is just a PHP wrapper for PDF libraries already present on the server so should have the same availability to exec().
Haven't had a chance to go through the code of fpdf yet but as I am a noob to PHP I don't think that is going to be particularly enlightening.
Please help!
EDIT:
As requested
FPDF
$pdf->Output('testdocument.pdf', 'F');
http://www.fpdf.org/en/doc/output.htm
Imagmagick
exec('convert "docprev.pdf[0]" -colorspace RGB -geometry 200 "document2.png"');
So those are the output commands of FPDF and Imagemagick respectively.

Mac OSX Convert library (html file to pdf) works via terminal but not PHP

I'm trying to take a generated html file and convert it to PDF on the fly with PHP.
It's going on my localhost and staying there so for the PDF conversion I'm using a Mac OSX utility, I guess you would call it.
The terminal command being:
/System/Library/Printers/Libraries/convert -f temporary.html -o destination/final.pdf
This works properly via terminal (produces a simple 20kb PDF file); however, when I run it inside PHP via passthru() the file (final.pdf) is created though it is a Zero KB file (corrupt).
The /destination folder is CHMOD 777, temporary.html is CHMOD 755.
Any help would be greatly appreciated.
Edit (Additional Detail):
Now in the error log, amongst the debug lines there is an error of "ERROR: xhtmltopdf (PID 13204) crashed on signal 6!"
I like to share what I do to generate PDF file on the fly. We use UNIX server to host.
I use tcpdf - a php library to convert HTML to PDF file. I use it in my projects and it works very well. TCPDF supports css, javascript, forms, images and lot more.
Website: http://www.tcpdf.org/
Demos: http://www.tcpdf.org/examples.php
When I need convert HTML in PDF I use this very nice software: http://www.princexml.com
You could have a look, it's free for personal use.

Categories