开发者

php to pdf or php to jpeg library or function?

开发者 https://www.devze.com 2022-12-09 16:29 出处:网络
I have trying to achieve like this. I have one page which is written wi开发者_StackOverflow中文版th php. There will be one button, which i click that button, that page will be converted to imageformat

I have trying to achieve like this. I have one page which is written wi开发者_StackOverflow中文版th php. There will be one button, which i click that button, that page will be converted to imageformat or pdf. Which function can i use? Is there any built it ? Which one will be more easier ? Creating imageformat or pdf? Please kindly point me out. Thanks.

With Regards,


You can use DOMPDF for generating PDF files. I've used it in almost every project I have that involves PDFs.


Use phpToPDF API

<?
include_once('phpToPDF.php');
$html = '<html><head></head><body>contents of a report.....</body></html>'; 
codetopdf($html,'/my_directory/','my_pdf_filename.pdf');

?>


You're looking at the PHP GD Library: http://php.net/manual/en/book.image.php

Using GD Library, you can use functions to work with image resources then output the final JPEG/PNG/GIF (or other image formats supported) to the browser or save it to a file.

Example from http://www.php.net/manual/en/function.imagecreate.php:

<?php

header("Content-type: image/png");
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);

?>

JPEG generation is much better than PDF since the file size is already smaller than PDF. Also, PDF requires the Adobe Acrobat Reader plugin to the browser in order for the browser to open, unlike JPEG which is already viewable by the browser.

Also, GD extension have a much more user base than other PDF extensions.


The GD library needs to be installed as an extra option and so may not be available. You would need to reinstall php using --with-gd[=DIR], where DIR is the GD base install directory.

There are a number of libraries you can use fpdf being one that i have used on several projects with success. It has the advantage of not needing to be installed /compiled just included. Documentation and examples are also good.


In my opinion, it's up to you which one you use. I would prefer some PDF extension, since PDF is a very common document exchange format. For that use I would Zend_Pdf which is still being forwards developed by the proffesional Zend engineers.

But both choices take you a lot of handwork, because you have to specify a concrete (x,y) position of every element in the text.

So - if you choose PDF, then take Zend_Pdf , otherwise GD library for jpegs.


As said, GD is a fine library to generate Jpeg images.
To answer your secondary questions as comments, it won't fetch data from SQL, that's the role of the PHP code surrounding the GD drawing calls.
GD, and the various PDF libraries (FPDF is quite well known and used, UFPDF and TCPDF are alternatives to manage Unicode chars, ) are more like drawing libraries, they don't handle HTML rendering, you have to position the characters, specify their style, etc.

Ah, dompdf have been mentioned too, looking again at it, I see it is an HTML to PDF library, which is closer of what you are looking for. Except it uses PDFLib which is a commercial, binary (last time I checked) library, hard to install on a shared server. But it can also use CPDF which seems easier to use (but slower).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号