Generate PDF with PDF-Lib

Hello,
I’m trying to do something simple. I have a button and when it is clicked i want to create a PDF and it must download. The code is almost the same from this page ( Create Document (pdf-lib) - JSFiddle - Code Playground )

Backend
export async function button1_click ( event ) {

const  pdfDoc  =  **await**  PDFDocument . create (); 
const  timesRomanFont  =  **await**  pdfDoc . embedFont ( StandardFonts . TimesRoman ); 
const  page  =  pdfDoc . addPage (); 
const  {  width ,  height  } =  page . getSize () 
const  fontSize  =  30 ; 
page . drawText ( 'Creating PDFs in JavaScript is awesome!' , { 
    x :  50 , 
    y :  height  -  4  *  fontSize , 
    size :  fontSize , 
    font :  timesRomanFont , 
    color :  rgb ( 0 ,  0.53 ,  0.71 ), 
}); 
const  pdfBytes  =  **await**  pdfDoc . save (); 

download ( pdfBytes ,  " pdf-lib_creation_example.pdf " ,  "application/pdf" ); 

}

Here is the problem. The last line (download) is not executed so I cannot get the PDF. I added the npm downloadjs (v1.4.7) and included this line at the begining import { download } from ‘downloadjs’ ;

What am I missing?

I’m not sure how you have button click function firing from a backend file like that. Is there more context missing here?

I would also check and see how that NPM is downloading by reading through the code, if it requires access to the DOM it will not work with native velo but perhaps in a custom element or HTML component.

There are many forum posts about pdf generation and some community made tutorials if you search the posts too that could help here.

Can you please provide a link to one of the posts that actually work inside WIX. Thank you.

I managed to create and download a PDF on a button click using the npm package pdf-lib in combination with the WIX Velo MediaManager API. It is free to use.

I hope my tutorial helps :slight_smile: