Rotate a page with pdf-lib and javascript

1.7k Views Asked by At

I am making a website that can modify pdf files , and I was trying to be able to rotate a page or multiple pages but couldn't find how to do that in their docs .


 const pdfDoc = await loadSingleFile(inputFiles[0]) // An input pdf file

 const rotatedPdf = await PDFDocument.create();  // Intitialize the rotated pdf file

 const pageToRotate = pdfDoc.getPages()[pageIndex]
    

  // Not sure what to write here


 const pdfDataString = await rotatedPdf.saveAsBase64({ dataUri: true }); // here generate string for further use
1

There are 1 best solutions below

0
On BEST ANSWER

I was able solve this following a comment on my question that suggested using these two functions

https://pdf-lib.js.org/docs/api/classes/pdfpage#getrotation to get the current page rotation degree.

https://pdf-lib.js.org/docs/api/classes/pdfpage#setrotation to rotate a page.