📜 ⬆️ ⬇️

And one more way to generate PDF

Inspired by the topic of PDF generation with ghostscript and Inkscape.

Looking at the proposed option, I remembered one very interesting solution, which I would like to share.

So, PDF generation with QT
')

I will not repeat what PDF is, I think everything is already up to date. Immediately get down to business.

What we need:



- Dedicated server with the ability to install their applications on linux.
- QT 4.4 library with qt-webkit module. This is the brain of our application.
- Initial code or already compiled project wkhtmltopdf . This is the heart of our application.
- Xvfb Xvfb is a small substitute for the X server. Resources does not eat, but does not show anything. But performs the functions we need.

A bit of theory:



We are going to generate PDF from HTML by rendering the HTML page using the HTML rendering engine webkit and printing it on a virtual PDF printer.

Prepare the system:



1. We start Xvfb in order that our console application, though not required, should be connected to X11
# / usr / bin / Xvfb: 0 -screen 0 1024x768x24 -ac

Here it would be necessary to register this business in autoload, but I will not advise it since Linux has all the different and there are a lot of options to do this.

2. I hope that QT is already installed in the system. A wkhtmltopdf downloaded or compiled with his own hand.
Install it in / usr / bin / wkhtmltopdf

3. Test.
#DISPLAY = ": 0.0" wkhtmltopdf habrahabr.ru habr.pdf

Must earn :)

We write the shell.



Since the Zend Framework is used for writing my current project in full, the component will be an extension to it.
More about this you can write an entire article. I will hope that those who still have not picked it up, will finally find the strength to do it.

Here is a class:
<? php

class Zfe_PDFGen {

const DISPLAY = ': 1.0' ;
const CACHEDIR = '/ application / cache /' ; // From Document Root
const EXEC = '/ usr / bin / wkhtmltopdf' ;

private $ _source ;

private $ _destination ;

function Zfe_PDFGen ( $ aSource , $ aDestination ) {

$ this -> setSource ( $ aSource );
$ this -> setDestination ( $ aDestination );

}

public function setSource ( $ aSource ) {
$ filename = new Zfe_Uuid ();
$ tmpfile = $ _SERVER [ 'DOCUMENT_ROOT' ]. self :: CACHEDIR . $ filename ".html" ;
file_put_contents ( $ tmpfile , $ aSource );
$ this -> _source = $ tmpfile ;
}

public function setDestination ( $ aDestination ) {
$ this -> _destination = $ _SERVER [ 'DOCUMENT_ROOT' ]. $ aDestination ;
}

public function generate () {
$ result = '' ;

$ exec = sprintf ( 'DISPLAY = "% s"% s% s% s' , self :: DISPLAY , self :: EXEC , $ this -> _source , $ this -> _destination );

exec ( $ exec , $ result );

unlink ( $ this -> _source );

}

}


I note that Zfe_UUid is used to generate the file name (random unique identifier according to RFC 4122 ), you certainly won't have it, but I think it will not be difficult to write your own generator.

Usage example

<? php
$ fetchview = new Zend_View ();
$ fetchview -> addScriptPath ( $ this -> view -> getScriptPaths ());
$ result = $ fetchview -> render ( 'index / _mailpdfgen.phtml' );

$ data = $ result ;

$ PDF = new Zfe_PDFGen ( $ data , '/html/files/test.pdf' );

$ PDF -> generate ();



That's all


What is better / more convenient? Well, at least by the fact that PDF is now generated from the “native” and “understandable” format for any typesetter.
And most importantly the generated PDF looks pretty damn cute. It works fast.

Cons, of course, also missing.
- Not everyone has their own server.
- exec ()
- in PDFFoc templates, paths to images in tags and the like, you have to write complete or relative to the template file. Which is not very convenient.

Source: https://habr.com/ru/post/30018/


All Articles