Issue with Jpeg's Orientation Exif when embedding in pdf
See original GitHub issueWhat were you trying to do?
I am trying to create a pdf file from multiple jpeg files.
How did you attempt to do it?
We allow the user to upload jpeg, png, pdf files in our application. They can then select those files to “join” them, which creates a pdf. This works fine, except when the jpeg has an Orientation exif, it is not taken into account by the pdf-lib, and they see the jpeg as being rotated when they create the pdf.
const newPdf = await PDFDocument.create()
const appendNextCheckedDocument = async (newPdf, documentToAppend, previewURL) => {
const documentBytes = await (await fetch(previewURL)).arrayBuffer()
if (documentToAppend.content_type.match('image/png') || documentToAppend.content_type.match('image/jpeg')) {
const image = await (documentToAppend.content_type.match('image/png')
? newPdf.embedPng(documentBytes)
: newPdf.embedJpg(documentBytes))
const page = newPdf.addPage([image.width, image.height])
page.drawImage(image, { x: 0, y: 0, width: image.width, height: image.height })
}
}
This works well with files that to not have an orientation exif.
What actually happened?
What happened is that the generated pdf contains image that look like they are rotated (they are not rotated, it is just that the Orientation exif is not taken into account, and they look rotated to the user).
You can find a user report here, unfortunately it is in French: https://community.doctolib.com/t5/logiciel-médical/bug-import-des-photos/m-p/72835
You can see that the user imports pictures taken from his phone on the left side of the screenshot (so, with orientation exif since they took it in portrait mode), but when the pdf is created (right side of the screenshot) the pictures seem rotated.
What did you expect to happen?
I would expect the orientation exif to be taken into account, so that the jpeg is rotated accordingly when appending it to a pdf file. Or, if I had an option that I could pass as a parameter, I could extract the exif orientation metadata and pass it to embedJpg.
How can we reproduce the issue?
You can use embedJpg with a file that has an orientation exif metadata. To get that you can take a picture with your phone and orient the screen while doing it, or you can use some examples here: https://github.com/recurser/exif-orientation-examples
Version
1.17.1
What environment are you running pdf-lib in?
React Native
Checklist
- My report includes a Short, Self Contained, Correct (Compilable) Example.
- I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
Thank you!
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:15

Top Related StackOverflow Question
Didn’t try but can you check if a package like this would do the job for you? https://github.com/onurzorluer/exif-auto-rotate
found solution. it solved my cases. this thread and discussion helped me to find correct way. thank you guys https://github.com/Hopding/pdf-lib/issues/1329#issuecomment-1285534542