![]() TheOutput.text('Some awesome example text'. The text method actually takes in an optional option property after the string that can allow us to do things like bold, underline, oblique (“italicize”), or strike. We would need to write quite a bit more code to format it. TheOutput.text('Some awesome example text')Īfter that we then get a pdf that would look like this:Īgain, that’s still pretty bland. We can add text using the text method like this: const PDFGenerator = require('pdfkit') TheOutput.pipe(fs.createWriteStream('TestDocument.pdf'))Īt this point, we would have a really useless pdf file that doesn’t contain anything. pipe to a writable stream which would save the result into the same directory We then need to instantiate the library and then pipe it to the writeable stream. ![]() const pdfGenerator = require('pdfkit')Īfter that, we can start to simply use it with the core file system (commonly called fs). Using PDFKit in an existing project is pretty easy.įrom the command line / terminal we need to add it is a dependency npm i pdfkitĪfter that we can use it as a module in our project by simply requiring it. PDFKit is a library that I’ve used a few times for generating very simple invoices that a template wasn’t used for. In this post, we will cover using a library to generate the pdf using just JavaScript. We are going to cover both methods though because there are times I prefer to write a lot of code and do it. Basically – I download the existing template and sales or marketing provides me the updates. I normally prefer to use a template that is HTML based to create pdf files because this potentially allows marketing or business users to make some of the needed changes without necessarily requiring code changes to happen. The first way is to use a PDF generation library that uses straight JavaScript/Nodejs and everything is untemplated or the other way is to use something templated in a language like HTML and then generate the pdf from it. There are two major ways to approach creating a PDF file from Nodejs. ![]() It’s a really common request for businesses to be able to export a document or data into a really well-formatted PDF file whether it be an invoice or a report. Use the size property to specify a string which is the name of the paper size.The PDF format is one of the most common file formats for transmitting static information. To use any of these predefined page sizes, pass an object to either the PDFDocument constructor or the addPage() method. The PDFKit text documentation has a complete list of these standard fonts. PDFKit supports each of the 14 standard fonts defined by the PDF format. To move up or down a line, call the moveDown or moveUp method with the number of lines the text should move: doc.moveDown( 3) īy default, calling either method without passing an argument will cause it to move by just a single line. However, by providing X and Y coordinates to the text method after the text itself, you can change its position: doc.text( 'Coding is Easy!', 100, 100) ![]() So, whenever you call the text method, PDFKit will add a new line beneath the previous one. ![]() To add text to the document, simply call the text() method and pass the text that you want to add as the argument: doc.text( "Coding is Easy!")Īs PDFKIT adds text to the document, it keeps track of that text's current X and Y positions. PDFKit makes adding text to documents simple and includes many options to customize the display of the output. Before you pipe your document to save it, you can make various changes. ![]()
0 Comments
Leave a Reply. |