How to Create PDF Files in Deno Using PDF-LIB Library Full Tutorial with Example

How to Create PDF Files in Deno Using PDF-LIB Library Full Tutorial with Example

 

 

 

Welcome folks today in this blog post we will be creating pdf files in deno using pdf-lib. This is will be a full tutorial example with source code.

 

 

Requirements

 

 

You should have Deno installed on your system before you proceed

 

 

Get Started

 

 

Now make a app.ts file inside your root directory and copy paste the following code

 

 

 

import { PDFDocument } from 'https://cdn.skypack.dev/pdf-lib@^1.11.1?dts';

// Create a new PDFDocument
const pdfDoc = await PDFDocument.create();

// Add a page to the PDFDocument and draw some text
const page = pdfDoc.addPage();
page.drawText('Creating PDFs in Deno is awesome!', {
  x: 100,
  y: 700,
});

// Save the PDFDocument and write it to a file
const pdfBytes = await pdfDoc.save();
await Deno.writeFile('create.pdf', pdfBytes);

// Done! 💥
console.log('PDF file written to create.pdf');

 

READ  How to Disable Right Click on Images to Prevent Save as and Download Dialog in jQuery Using Javascript

 

 

Now if in order to execute this script by deno you need to execute this following command which is shown below.

 

 

deno run --allow-write app.ts

 

 

 

 

 

 

Now there is a flag there --allow-write which is required in the command to allow write access to deno in order to create new files on the system.

 

 

Now if you check the root directory a create.pdf will be created which is shown below.

 

 

READ  Build a Unit Weight Converter (Kilograms + Pounds + Ounces + Grams) in Javascript Full Project For Beginners

 

 

Now if you open this file you will see the following result

 

 

 

 

Leave a Reply