Php generate pdf from html. Comparison of methods for generating pdf from html


Generating pdf documents is a daily task in web development. The list of such documents includes invoices, invoices, policies and others. There are many ready-made libraries for solving this problem, including for php. For example, mpdf, tcpdf, and many others. The file can be compiled using the api of these libraries, but this is a rather lengthy task. There’s never too much time to complete a task, right? Therefore, most often a pdf file is created from an html representation, which is quite convenient. But, unfortunately, not everything is so simple. This approach has many pitfalls that can irritate anyone.

For example:

  • Styles cannot be included separately, therefore they must be included in the HTML document as a separate block, or inline for each element. There's nothing wrong with this, just a minor inconvenience.
  • Unfortunately, in such libraries some styles may not work as expected, or may not work at all. This is the main drawback.
  • From the previous paragraph it follows that creating a document that best meets the requirements is very labor-intensive, and sometimes simply impossible.
  • And in the case of development for Bitrix, there is one more problem. Everyone knows that for the platform to work, it is necessary to set the mbstring.func_overload parameter in php.ini to the value 2. And to create a pdf file containing the Cyrillic alphabet, the value 0 is required. Usually this problem is solved by setting up a web server, but it is still unpleasant.

Faced with such a task once again, I increasingly think that a separate microservice should handle the generation of pdf files, especially if the project is large, but that’s a completely different story.

It's time to move on to the main part of this article. In addition to the options proposed above, there is an alternative - PhantomJS.

PhantomJS is a build of the WebKit engine without a graphical interface that allows you to load a web page in console mode, execute JavaScript, and fully work with DOM, Canvas and SVG.

Of course, in addition to the features listed above, it allows you to create PDF files.

How? In simple terms, it downloads the required web page and gives you the option to save the result as a pdf file.

The PhantomJS installation process is described in sufficient detail in the documentation, so I will not dwell on this issue.

An important point in the work of PhantomJS is the js file (hereinafter referred to as config.js), which by its content determines what exactly we want to do. The official website has many ready-made examples of such files, with the help of which a variety of tasks are solved, for example, unit testing. Examples include saving a web page in pdf format. This is exactly what we need.

A couple of small edits will allow us to use this example for our purposes.

var page = require("webpage"). create(), system = require("system"), address, output, size; //if there are few or too many arguments, a message is displayed with help on usage if (system . args . length< 3 || system . args . length >5) (console.log( "Usage: config.js URL filename"); console. log( " paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter""); console. log( " image (png/jpg output) examples: "1920px" entire page, window width 1920px"); console. log( ""800px*600px" window, clipped to 800x600"); phantom. exit(1); ) else ( // processing arguments address = system . args[1]; output = system . args[2]; page. viewportSize = (width: 800, height: 800); if (system . args . length > 3 && system . args [ 2 ]. substr (- 4 ) === ".pdf" ) ( size = system . args [ 3 ]. split ("*" ); page . paperSize = (size . length === 2 ) ? ( width : size [ 0 ], height : size [ 1 ], margin : "0px" ) : ( format : system . args [ 3 ], orientation : "portrait" , margin : "1cm" ); ) else ( console . log ( "Invalid path to pdf!" ); phantom . exit ( 1 ); ) if ( system . args . length > 4 ) ( page . zoomFactor = system . args [ 4 ]; ) // opening the page and saving the result page. open (address , function (status) ( if (status !== "success" ) ( console . log ("Unable to load the address!" ); phantom . exit (1 ); ) else ( window . setTimeout (function ( ) ( page . render ( output ); phantom . exit (); ), 200 ); ) )); )

$ phantomjs path/to/config.js "url" path/to/pdf/file "A4"

Let's look at the arguments in order:

  1. Path to config.js.
  2. The address of the web page that needs to be converted to a pfd file.
  3. Path to the pdf file in which the result will be saved.
  4. PDF file format.

We get the required pdf file.

Coming back to php, this solution is quite easy to integrate into your code.

In the simplest case it looks like this:

$command = sprintf( "phantomjs %s %s %s %s", $fullPathToConfigJS, $url, $fullPathToSave, $format ); exec($command);

So, let me summarize. In my opinion, this solution has the following advantages:

  • Make a layout for such a document much easier. This is very important because... not only simplifies development, but also does not turn subsequent document edits into a nightmare.
  • Eliminates the need to mess around with mbstring.func_overload. But this is undoubtedly a problem typical mainly for Bitrix.

Of course, there are also disadvantages:

  • Not 100% support for all CSS styles. But in comparison with the libraries listed at the beginning of the article, everything is very good.
  • This solution can scare away a novice developer.
03.12.2015

PDF24 Creator is a free PDF generator for your PC. Generating PDF files is incredibly easy with this program. The combination of additional features along with the creation of PDF files makes this tool one of the most recognized free programs. Alternatively, you can also generate PDF online. Read below how it works.

PDF24 Creator – free PDF generator

PDF24 Creator is a free PDF generator for Windows. Generation of PDF files is realized using a PDF printer. After installation, a new virtual PDF printer will appear in your Windows. Just print anything you want to turn into PDF through this printer. PDF printer is the only way to create PDF. The program includes a wide range of features that will help you in PDF generation. This program is free. You can download the latest version from this site.

PDF24 Online PDF Generator – Generate PDF files online

Online PDF Generator is a useful tool if you want to convert documents into PDF format. Simply select a document or send a document by mail to the Online PDF Generator and in a few seconds your PDF file will be available for download or sent back to you.

An email interface for this PDF generator is also available. You need to send the files to the generator and wait for a response letter, which will contain the files converted to PDF

Many other PDF generators in the PDF24 toolkit

Online utilities from PDF24 solve many PDF-related problems quite simply, quickly and free of charge. Many of these tools generate PDF files. Are you not yet familiar with PDF24 online utilities? Take a look, you can use all these tools.

One of the important parts of any project management system is the ability to report on the current state of affairs. This allows you to more accurately assess the current state of affairs. For users, such a report can be not only analytics necessary for work, but also a unique form of reporting. Therefore, Revizto pays great attention to the technical side of report generation.

For some clients, reports are large pdfs with tens of thousands of pages. Of course, such reports weigh gigabytes and their generation takes a long time. Faced with another increase in the size of the report, we decided to analyze our pdf generation architecture.

Traditionally, pdf generation on the web consists of two stages:

  1. Generating an html page, which will later become a pdf
  2. Translation of html to pdf

The first stage in our case took quite a bit of time, but the second could last more than an hour. Therefore, we made a small comparison of what open source opportunities exist for generating pdfs:

    Command being timed: "phantomjs --ssl-protocol=any ./capture.js 1.html 1.pdf" User time (seconds): 2240.56 System time (seconds): 6.96 Percent of CPU this job got: 83% Elapsed ( wall clock) time (h:mm:ss or m:ss): 44:35.89 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes) ): 0 Maximum resident set size (kbytes): 1821840 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 28 Minor (reclaiming a frame) page faults: 489364 Voluntary context switches: 53462 Involuntary context switches : 31797 Swaps: 0 File system inputs: 4576 File system outputs: 233848 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0

    Command being timed: "/usr/bin/google-chrome-stable --headless --disable-gpu --print-to-pdf 1.html" User time (seconds): 54.22 System time (seconds): 7.32 Percent of CPU this job got: 5% Elapsed (wall clock) time (h:mm:ss or m:ss): 18:49.74 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size ( kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 951796 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 2 Minor (reclaiming a frame) page faults: 938614 Voluntary context switches: 184497 Involuntary context switches: 37463 Swaps: 0 File system inputs: 368 File system outputs: 174352 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0

  1. Wkhtmltopdf Command exited with non-zero status 1 Command being timed: "wkhtmltopdf 1.html 1.pdf" User time (seconds): 3356.13 System time (seconds): 7.12 Percent of CPU this job got: 99% Elapsed (wall clock) time (h:mm:ss or m:ss): 56:21.54 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 3457744 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 8 Minor (reclaiming a frame) page faults: 977440 Voluntary context switches: 38567 Involuntary context switches: 47975 Swaps : 0 File system inputs: 1240 File system outputs: 197480 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 1

From the above test it is clear that the best result is obtained from chrome, then phantomJs is twice as bad, and then Wkhtmltopdf is even much worse. This result echoes problems with phantomJs development

On the other hand, chrome depends on X11, so there will be significantly more issues with it on the server than with phantomJs.

If we draw a small conclusion, then for testing a machine with a chrome desktop is the undisputed winner. It makes sense to use it on a server if you are not afraid to put X11 there, otherwise phantomJs will be the best option

Where can this be useful? If you need to generate print-ready files in a web application using an existing arbitrary rigid template: certificates, badges, passes, etc.

Why PDF? The PDF format allows you to create documents with a number of undeniable advantages: openness, cross-platform, widespread and, most importantly, the accuracy and immutability of data transfer along the chain of creation, viewing and printing.

What's the salt? Using SVG files as templates with the ability to substitute the required fields and then convert to PDF.

What are the benefits? The ability to create and quickly edit very complex templates in familiar vector editors such as Adobe Illustrator, Corel Draw or Inkscape. Easy to program and use only free software. Another important advantage is the ability to transparently use UTF-8 for pasted texts.

What is needed for that? To use this method, you need a dedicated server with the ability to install your own applications (Inkscape and GhostScript) and execute system commands. In this case, everything will work both on the Windows platform and on Linux.

I think the short FAQ has covered the main issues regarding this method, so let’s immediately begin to analyze its essence.

As you know, the SVG vector graphics format is actually an XML file, so an already created file can be easily edited using the simplest programming tools. If you use an SVG file as a rigid template, the process is greatly simplified, because we do not need to change the structure of the document, but only need to substitute the necessary text values ​​or base64 encoded bitmaps.

You can create the initial template in any vector editor that supports export to svg: Adobe Illustrator, Corel Draw or in Inkscape itself. The use of the latter is desirable, at least at the last, finishing stage, since, ultimately, it is he who will have to carry out the transformation we need.

When using a raster in a template, you can use 2 methods: store the raster in a separate external file or embedded in the SVG file itself. If you need to change the raster image in the template in the first case, you can also change the file before generation. When storing a picture embedded in a file, you should write the following line in the URL property of the picture object:

where (IMAGE) is the field for the template engine to insert a base64 encoded image.

For example, let’s draw a simple badge template, I think you’ll forgive me for being crooked, I’m not an artist, but for real use, you can order a vector layout from your designer.


I didn't use a resizable bitmap, leaving that to homework, and limited myself to just resizable text fields.

I think you have already noticed that template engine tags are inserted in the places where the expected text is supposed to be written (in this example we used ). It is the use of XML compatible tags that makes it possible to write them in the vector editor itself without resorting to additional editing.

We have a template, and we can insert the data we need without any problems, but how will we actually carry out the conversion? To do this, we'll use the Inkscape command line interface:

#convert to PDF
inkscape -A

Using the key " -A“We will immediately receive a PDF file, but, unfortunately, the PDF created directly is very large. To solve this problem, you can take a workaround. Namely, use SVG export not directly to PDF, but along a chain SVG->PS->PDF. By using the ps2pdf utility from the Ghost Script kit to generate the final PDF file, we can reduce the size of the final file by tens of times.
#convert to PostScript file
inkscape -P
#convert PostScript file to PDF
ps2pdf

The only downside is that in this case we will lose all transparency effects, since the PostScript format does not support it.

For complete portability of generated documents, you can add the option “ -T» Convert all text to curves. This way we can get rid of problems with the presence of fonts on the client machine, as well as problems with encodings.

Now we have everything we need: an SVG template and transformation commands. Let's write a PHP script that would output a pdf file generated from a template.

/* ****************************************************************************************
* Script for generating a pdf pass file using sequential conversion
* template into an svg file, after which it is converted to a PostScript file by Inkscape,
* and the latter is converted to pdf using the ps2pdf utility.
*
* Author: Shebastyuk V.V. a.k.a. JStingo
* **************************************************************************************** */

/* script parameters */

//Path to the folder with temporary files
//(if not specified, the files will be stored in the system temporary folder)
$tmp_dir = "" ; //generate paths to temporary svg, ps and pdf files
$tmp_svg_file = tempnam ($tmp_dir, "" );
$tmp_ps_file = tempnam($tmp_dir, "");
$tmp_pdf_file = tempnam($tmp_dir, ""); /* Templating engine FastTemplate */
include( "include/cls_fast_template.php");
$tpl = new FastTemplate("templates");try( /* Block with data received for templating */
/* ........................... */
$user_name = "JStingo" ;
$register_date = "28/09/2007" ;
/* ........................... */
/* form the name of the resulting file in the form User_name.pdf */
$pdf_file_name = $user_name . ".pdf" ; /* processing the template and getting the resulting file */$tpl -> define (array("svg" => "template.svg" ));
$tpl -> assign (array("USER_NAME" => $user_name ,
"R_DATE" => $register_date
));
$tpl -> parse ("SVG" , "svg" ); //save the resulting svg file
$tpl -> FastWrite ("SVG" , $tmp_svg_file ); //convert the svg file using inkscape to a ps file
//Keys
// -T - serves to convert text to curves (for normal font support)
// -P - indicates the need to convert to a PostScript file
system( "inkscape -T $tmp_svg_file -P $tmp_ps_file", $success );if($success != 0 )
throw new Exception ( "Error generating ps file.");//convert the ps file to pdf using the ps2pdf utility

//Keys
// -dUseFlateCompression=true - sets the use of compression
// -dPDFSETTINGS=/printer - sets optimization for printing

system( "ps2pdf -dUseFlateCompression=true -dPDFSETTINGS=/printer $tmp_ps_file $tmp_pdf_file", $success ); //if the conversion fails, throw an exception
if($success != 0 )
throw new Exception ( "Error generating pdf file.");// header stating that we will send a pdf file
header ( "Content-type: application/pdf");// Will be called like $pdf_file_name
header ( "Content-Disposition: attachment; filename="". $pdf_file_name . """ ); // transfer the generated file
readfile($tmp_pdf_file); //delete temporary files
@unlink($tmp_svg);
@unlink($tmp_ps_file);
@unlink($tmp_pdf_file);catch(Exception $e)(
/* If an error occurs somewhere, we report it */
$tpl -> define (array("error" => "error.tpl" ));
$tpl -> assign ("ERROR" , $e -> getMessage ()); $tpl -> parse ( "ERROR" , "error" );
$tpl -> FastPrint("ERROR");
}
?>

I think it won’t be too difficult for anyone to rewrite the script in another programming language.

Online or offline?

Create PDF files from any application. Work online using our online app or offline by downloading the Soda PDF desktop app to your computer.

Create a file

If you choose to create a PDF using an online application, upload an existing file from your computer or from a cloud storage service such as Google Drive or Dropbox.

Send by mail

Once the PDF file is created, you can send it to your email address or download it to your computer and view it in your browser.

DID YOU KNOW?

There is a new, eco-friendly way to print documents!

There's no need to use paper when you can use virtual printing. When you download the Soda PDF desktop app, it will install the Soda PDF virtual printer on your computer. You can then select the Soda PDF printer from any program to convert any document to PDF without opening the Soda PDF desktop app! When you're ready to print your document, simply select the Soda PDF printer from the printer menu and it will create a PDF copy of the document. With Soda PDF you can create PDF files from over 300 file formats.

ADDITIONAL INFORMATION ABOUT PDF CREATION

PDF format

The PDF format was created by Adobe Systems in 1993. In 2008, it was standardized as an open format, developed by an independent ISO committee.

PDF 2.0

The PDF format has evolved significantly since its inception. The most recent version is PDF 2.0, the standard of which was published on July 28, 2017.

Other options

If you want to create a PDF file by merging multiple files, use our free online Merge PDF tool. You can even choose the order in which individual files appear in the overall file.







2024 gtavrl.ru.