📜 ⬆️ ⬇️

Wkhtmltopdf + Node.JS

Creating pdf documents is a fairly common task. It is successfully coped with by a whole family of libraries, which allow literally “assemble” pdf in parts or fill it in on the basis of a previously prepared template. This approach is reliable, because we can count on the fact that, having changed the text of one inscription, pagination on some pages will not disappear. On the other hand, adding new pages to pdf takes some time for the developer and, the more different visual elements, the more time it takes.

However, there is another way to create pdf documents: converting from some markup language using the appropriate tool. This method will be effective and take less time to make changes to pdf, if the selected tool works quite predictably. There are several similar solutions, but in our project we chose Wkhtmltopdf , which generates a pdf document from HTML. After a year of using this tool, I can say that the choice was made right, because all needs were covered with head.

In this article I want to share libraries that allow you to simplify the work with wkhtmltopdf in Node.JS.

Currently there are several packages in npm that allow you to integrate wkhtmltopdf. However, they have their disadvantages:
  1. All options to wkhtmltopdf are passed as an object. The wkhtmltopdf has a lot of options and you need to constantly run between the documentation and the code to form the correct object, so I would like to have hints from the IDE when completing the options.
  2. Not all wkhtmltopdf functionality is covered by most libraries. For example, not everywhere there is the possibility of generating pdf documents from several HTML sources or it is not possible to configure the generated table of contents (table of contents)

')
As a result, wkhtmltopdf-nodejs-options-wrapper libraries were developed, which is a wrapper for wkhtmltopdf parameters, and wkhtmltopdf-nodejs-pdfapi , intended for creating pdf documents.

Consider an example of their use:

var wkhtmlToPdfOptions = require('wkhtmltopdf-nodejs-options-wrapper'), PdfApi = require('wkhtmltopdf-nodejs-pdfapi'); var pdfApi = new PdfApi(), request = new wkhtmlToPdfOptions.CreateRequest(); //    var googlePage = new wkhtmlToPdfOptions.Page(); googlePage.setInput('http://google.com'); //    var habrPage = new wkhtmlToPdfOptions.Page(); habrPage.setInput('http://habrahabr.ru'); habrPage.getOptions().setZoom(0.5); //   50% request.addPage(googlePage); request.addPage(habrPage); request.getGlobalOptions().setImageDpi(300); //     300dpi request.getHeadersAndFooterOptions().setFooterCenter('Footer text'); //      // createPdf    pdf   promise pdfApi.createPdf(request, 'result.pdf') .then(function(data, debug) { console.log('Pdf  '); }, function(data, debug) { console.log(' : ' + data); }); 


As you can see from the example, to create a pdf document, you must create a CreateRequest object, fill it with the necessary data and transfer it to pdfApi .

IDE (WebStorm in my case) tells you which methods you can use: image



This functionality is already enough for use on the server, but it would be even more useful to be able to start creating pdf from the client. To do this, we need to have a web service that would accept requests and give ready-made pdf files.

As such a service, you can use the WebSocket server wkhtmltopdf-nodejs-ws-server , which can be easily launched as follows:

 var WsServer = require('wkhtmltopdf-nodejs-ws-server'); var server = new WsServer(3000); // <-   ""    *:3000 server.start(); 

Client code may look like this:

 //  webpack  browserify,      <b>require</b> var wkhtmlToPdf = require('wkhtmltopdf-nodejs-options-wrapper'), io = require('socket.io-client'); var socket = io('http://ip__:3000'); var page = new wkhtmlToPdf.Page(), request = new wkhtmlToPdf.CreateRequest(); page.setInput('http://google.com'); //  pdf     request.addPage(page); request.getGlobalOptions().setPageSize('Letter'); socket.on('pdf:create:success', function(response) { console.log('Pdf created: http://ip__:3000/result_' + response.handle + '.pdf'); }); socket.on('pdf:create:fail', function(response) { console.log('Pdf creation failed!'); console.log(response); }); socket.emit('create', request.toObject()); 


Everything. This is enough to create pdf documents.

I hope npm packages and examples given in the article will be useful.

Thanks for attention.

PS Documentation: wkhtmltopdf-nodejs-options-wrapper , wkhtmltopdf-nodejs-pdfapi , wkhtmltopdf-nodejs-ws-server

Source: https://habr.com/ru/post/266171/


All Articles