HTML to PDF with Node.js

前端 未结 12 1922
终归单人心
终归单人心 2020-11-27 09:20

I\'m looking to create a printable pdf version of my website webpages. Something like express.render() only render the page as pdf

12条回答
  •  一个人的身影
    2020-11-27 10:01

    Package

    I used html-pdf

    Easy to use and allows not only to save pdf as file, but also pipe pdf content to a WriteStream (so I could stream it directly to Google Storage to save there my reports).

    Using css + images

    It takes css into account. The only problem I faced - it ignored my images. The solution I found was to replace url in src attrribute value by base64, e.g.

    You can do it with your code or to use one of online converters, e.g. https://www.base64-image.de/

    Compile valid html code from html fragment + css

    1. I had to get a fragment of my html document (I just appiled .html() method on jQuery selector).
    2. Then I've read the content of the relevant css file.

    Using this two values (stored in variables html and css accordingly) I've compiled a valid html code using Template string

    var htmlContent = `
    
    
      
        
      
      
        ${html}
      
    `
    

    and passed it to create method of html-pdf.

提交回复
热议问题