📜 ⬆️ ⬇️

HTML to PDF

html to pdf

Back in 2008, a similar article was already written and I tried to apply knowledge, but, unfortunately, I did not cope with the Russian language (I worked on denwer, there was no hosting). Perhaps it was the lack of experience. I recently found a good library and decided to share. The topic is most likely addressed to a novice programmer and doesn’t claim anything.

First, we set the task:


  1. The conversion process should be simple - a minimum of code;
  2. Display Russian out of the box;
  3. Understand html tags, images, CSS;
  4. Use a free and well-documented library.

Input data


The process itself is so simple that there is no fundamental difference in which framework to use it. Convert data from HTML to PDF will be using mPDF .

Implementation


Download and unpack the latest mPDF version in the root directory. In the same place, create the index.php file and add the following code:
<?php
$html = '<table_ border="1"><tr_><td_> 1</td_><td_> 2</td_><td_> 3</td_><td_> 4</td_></tr_>
<tr_><td_> 5</td_><td_> 6</td_><td_> 7</td_><td_><a_ href="http://mpdf.bpm1.com/" title="mPDF">mPDF</a_></td_></tr_></table_>';

include("mpdf50/mpdf.php");

$mpdf = new mPDF('utf-8', 'A4', '8', '', 10, 10, 7, 7, 10, 10); /* , ...*/
$mpdf->charset_in = 'cp1251'; /* */

$stylesheet = file_get_contents('style.css'); /* css*/
$mpdf->WriteHTML($stylesheet, 1);

$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html, 2); /* pdf*/
$mpdf->Output('mpdf.pdf', 'I');
?>


Css file code:
table {text-align: center;font-size: 20pt;width: 100%;}

The library can be checked on the official website . Or, for example, to form the above example.
')
That's all. Thank.

PS: Many thanks to habrowsers: rachiu, Zorkus, FeNUMe, Atrax, AusTiN for their support and human attitude towards beginners.

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


All Articles