Please tell me how to output PDF in zend framework2.

Asked 1 years ago, Updated 1 years ago, 124 views

I would like to renovate the web system built on zendframework2. Please tell me how to place the PDF output button on the web page (with multiple images) and press it to output the PDF.

Thank you for your cooperation.

[Notes]
System OS: Windows Server 2012R2 Language: PHP 5.6.3 (ZendFramework 2)
DB: Postgres

php zend-framework

2022-09-30 16:44

2 Answers

Similar to @mamor, there is something called Snappy(wkhtmltopdf).

https://github.com/KnpLabs/snappy

If $composer is not available

http://wkhtmltopdf.org/

I think it's okay to just do it.(By the way, wkhtmltopdf can be used as a command)

Just like Phantom.js, please note that the content on the web page may be missing information or the rendering results may vary.


2022-09-30 16:44

This answer assumes that the PDF you want to output is dynamic and cannot be addressed simply by statically placing the PDF file.Also, since it is a method of converting HTML to PDF, it is a prerequisite on the ZF2 side to have the original page for the PDF that you want to output.

First, PHP can generate PDFs natively.
http://php.net/manual/ja/book.pdf.php

But that's not the way to do it's natural.However, it can only be used in PHP and implementation may be troublesome.

PDF generation using an external library mainly uses a method of converting HTML to PDF.The following is an example:
https://github.com/dompdf/dompdf
https://github.com/tcpdf-clone/tcpdf

And with PhantomJS, HTML can be converted into PDFs regardless of language or framework.
http://phantomjs.org/

While DomPDF and TCPDF analyze HTML and CSS in the PHP process to generate PDFs (JS does not work), PhantomJS is a process on PhantomJS that uses HTML as a browser to generate PDFs (JS does).

The following
is described in http://phantomjs.org/screen-capture.html on the official PhantomJS website. "Beside PNG format, PhantomJS supports JPEG, GIF, and PDF."
This is the method of the .ZF2 should be aware of the last PHP samble.

First, download the PhantomJS for your operating system from http://phantomjs.org/download.html.Please pass the bus if necessary.For example, in the case of a Mac, you need phantomjs -{VERSION}-macosx/bin/phantomjs in the downloaded zip file, so you can move this file to /usr/local/bin/, and so on.If you can check the version as follows, it will be OK.

$phantomjs-v
2.0.0

Then download https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js.Save https://raw.githubusercontent.com/ariya/phantomjs/master/examples/rasterize.js from curl, wget, or browser.

This is the end of the preparation.Create and save PDFs in PhantomJS.Run the following command where you have downloaded rasterize.js.

$phantomjsrasterize.js http://example.com/ example.com.pdf

If "example.com.pdf" was created where the command was executed and the contents were http://example.com/, it was executed correctly.

Finally, the following is a sample of how to do this in PHP:

<?php

$pdf = uniqid().'.pdf';
exec('phantomjs rasterize.js http://example.com/'.$pdf);

header('Content-type:application/pdf');
echo file_get_contents($pdf);
unlink($pdf);


2022-09-30 16:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.