HTML to PDF with Node.js

前端 未结 12 1894
终归单人心
终归单人心 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条回答
  •  Happy的楠姐
    2020-11-27 09:57

    In my view, the best way to do this is via an API so that you do not add a large and complex dependency into your app that runs unmanaged code, that needs to be frequently updated.

    Here is a simple way to do this, which is free for 800 requests/month:

    var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
    var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
    
    // Configure API key authorization: Apikey
    var Apikey = defaultClient.authentications['Apikey'];
    Apikey.apiKey = 'YOUR API KEY';
    
    
    
    var apiInstance = new CloudmersiveConvertApiClient.ConvertWebApi();
    
    var input = new CloudmersiveConvertApiClient.HtmlToPdfRequest(); // HtmlToPdfRequest | HTML to PDF request parameters
    input.Html = "Hello, world!";
    
    
    var callback = function(error, data, response) {
      if (error) {
        console.error(error);
      } else {
        console.log('API called successfully. Returned data: ' + data);
      }
    };
    apiInstance.convertWebHtmlToPdf(input, callback);
    

    With the above approach you can also install the API on-premises or on your own infrastructure if you prefer.

提交回复
热议问题