HTML to PDF with Node.js

前端 未结 12 1928
终归单人心
终归单人心 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:05

    For those who don't want to install PhantomJS along with an instance of Chrome/Firefox on their server - or because the PhantomJS project is currently suspended, here's an alternative.

    You can externalize the conversions to APIs to do the job. Many exists and varies but what you'll get is a reliable service with up-to-date features (I'm thinking CSS3, Web fonts, SVG, Canvas compatible).

    For instance, with PDFShift (disclaimer, I'm the founder), you can do this simply by using the request package:

    const request = require('request')
    request.post(
        'https://api.pdfshift.io/v2/convert/',
        {
            'auth': {'user': 'your_api_key'},
            'json': {'source': 'https://www.google.com'},
            'encoding': null
        },
        (error, response, body) => {
            if (response === undefined) {
                return reject({'message': 'Invalid response from the server.', 'code': 0, 'response': response})
            }
            if (response.statusCode == 200) {
                // Do what you want with `body`, that contains the binary PDF
                // Like returning it to the client - or saving it as a file locally or on AWS S3
                return True
            }
    
            // Handle any errors that might have occured
        }
    );
    

提交回复
热议问题