Search code examples
javascriptreactjspdf-lib.js

Rotate a page with pdf-lib and javascript


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

Solution

  • 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.