Pdf guide PDFKit Guide By Devon Govett Version CGetting Started with PDFKit Installation Installation uses the npm package manager Just type the following command after installing npm npm install pdfkit Creating a document Creating a PDFKit document is qu

PDFKit Guide By Devon Govett Version CGetting Started with PDFKit Installation Installation uses the npm package manager Just type the following command after installing npm npm install pdfkit Creating a document Creating a PDFKit document is quite simple Just require the pdfkit module in your Co ?eeScript or JavaScript source ?le and create an instance of the PDFDocument class PDFDocument require 'pdfkit' doc new PDFDocument PDFDocument instances are readable Node streams They don't get saved anywhere automatically but you can call the pipe method to send the output of the PDF document to another writable Node stream as it is being written When you're done with your document call the end method to ?nalize it Here is an example showing how to pipe to a ?le or an HTTP response doc pipe fs createWriteStream ' path to ?le pdf' write to PDF doc pipe res HTTP response add stu ? to PDF here using methods described below ?nalize the PDF and end the stream doc end The write and output methods found in PDFKit before version are now deprecated CUsing PDFKit in the browser As of version PDFKit can be used in the browser as well as in Node There are two ways to use PDFKit in the browser The ?rst is to use Browserify which is a Node module packager for the browser with the familiar require syntax The second is to use a prebuilt version of PDFKit which you can download from Github Using PDFKit in the browser is exactly the same as using it in Node except you'll want to pipe the output to a destination supported in the browser such as a Blob Blobs can be used to generate a URL to allow display of generated PDFs directly in the browser via an iframe or they can be used to upload the PDF to a server or trigger a download in the user's browser To get a Blob from a PDFDocument you should pipe it to a blob-stream which is a module that generates a Blob from any Node-style stream The following example uses Browserify to load PDFKit and blob-stream but if you're not using Browserify you can load them in whatever way you'd like e g script tags require dependencies PDFDocument require 'pdfkit' blobStream require 'blob- stream' create a document the same way as above doc new PDFDocument pipe the document to a blob stream doc pipe blobStream add your content to the document here as usual get a blob when you're done doc end stream on ' ?nish' - get a blob you can do whatever you like with blob stream toBlob 'application pdf' or get a blob URL for display in the browser url stream toBlobURL 'application pdf' iframe src url You can see an interactive in-browser demo of PDFKit here Note that in order to Browserify a project using PDFKit you need to install the brfs module with npm which is used to load built-in font data into the package It is

  • 28
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager
  • Détails
  • Publié le Aoû 15, 2022
  • Catégorie Administration
  • Langue French
  • Taille du fichier 108.9kB