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