Nginx Recipes: HTML to PDF Conversion

To prepare the conversion from HTML to PDF, we need ourselves and its plugin html2pdf. (I gave links to my fork of nginx, because I made some changes that have not yet been able to push into the original repository. You can also use ready-made.)

To convert HTML to PDF from a file

location =/html_to_pdf_from_file {
    html2pdf on; # включаем pdf-фильтр
}

To convert HTML to PDF from text

location =/html_to_pdf_from_text {
    html2pdf on; # включаем pdf-фильтр
    return 200 "<p style="background-color: #c11">Здравствуй, мир!</p>"; # возвращаем HTML
}

To convert HTML to PDF from a proxy

location =/html_to_pdf_from_proxy {
    html2pdf on; # включаем pdf-фильтр
    proxy_pass somewhing_that_returns_html; # перенаправляем на туда, гду возвращается HTML
}

as a result, when accessing these locations, instead of HTML, a PDF generated based on it will be returned (using generator from wt).

This, of course, is not the best generator, I tried a couple more before it, but found errors in them htmldoc и wkhtmltopdf.

Source: habr.com

Add a comment