question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with Jpeg's Orientation Exif when embedding in pdf

See original GitHub issue

What 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:open
  • Created a year ago
  • Reactions:3
  • Comments:15

github_iconTop GitHub Comments

1reaction
sobrinhocommented, Aug 23, 2022

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

0reactions
ArkanRomanPangocommented, Oct 20, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image with EXIF orientation incorrectly oriented in PDF export
Summary: When I insert an image with an EXIF orientation tag to Inkscape and export the document as PDF, the EXIF tag will...
Read more >
Orientation issues when inserting jpeg image into pdf using fpdf
I wrote this at the top of my pdf making file. I've only tested it with portrait images. function rotateImage($filePath){ // the file...
Read more >
Image roated in PDF - Adobe Support Community - 11644725
We use WKHTMLTOPDF and were encountering the same issue. It was due to embedded rotation metadata in the image.
Read more >
PDF to SVG conversion keeps EXIF orientation tags in ...
Steps to reproduce with poppler 2021.05: ... This PDF contains an embedded JPEG image which has EXIF orientation tag set to a non-default...
Read more >
How to avoid auto rotation of images when drawing in PDF
The EXIF (Exchangeable Image File Format) standard specifies some set of tags that can be embedded in images. One of these tags specifies...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found