Skip to content

Images in PDFDoc are rotated #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
felimartina opened this issue Feb 27, 2017 · 9 comments
Closed

Images in PDFDoc are rotated #626

felimartina opened this issue Feb 27, 2017 · 9 comments

Comments

@felimartina
Copy link

felimartina commented Feb 27, 2017

output-pdf.pdf
lid1482875-3_laborreceipt_01

Hi! I've been dealing with this for a couple days already, I looked thru all issues and history in this repository but didn't find anyone else having the same issue.

So...All I do is to add an image to a PDFDoc that I am creating. This image is on portrait mode (which means that there is a field in EXIF that indicates that image is Rotated 90 degrees. However, when I look to the generated PDF the image shows landscape.

Any thoughts or ideas? Problem surely is that I am doing something wrong because haven't seen anyone else having that issue.

This is a taste of my code:

const DOCUMENT_DEFAULTS = {
  DEFAULT_MARGIN: 0,
  MARGIN_LEFT: 0,
  MARGIN_TOP: 20,
  DOC_WIDTH: 612, // Letter Size @ 72 DPI
  DOC_HEIGHT: 792, // Letter Size @ 72 DPI,
  IMG_WIDTH: 612,
  AUTHOR: 'COMPANY NAME'
}
/**
 * Generates a PDF file with the supplied images in the specified file
 * @param {string} outputFilename. Full path and filename where PDF file should be generated.
 * @param {object[]} documents. List of document objects ({filename: 'FULL_PATH', name: 'DOC_NAME'}) to include in the PDF
 */
module.exports = function (outputFilename, documents, title) {
// ...
      // Generate PDF document with all images and submit it to Carrier
      let pdfDoc = new PDFDocument({
        autoFirstPage: false,
        margin: DOCUMENT_DEFAULTS.DEFAULT_MARGIN,
        width: DOCUMENT_DEFAULTS.DOC_WIDTH,
        height: DOCUMENT_DEFAULTS.DOC_HEIGHT
      })
      // Add metadata
      pdfDoc.info = {
        Title: title,
        Author: DOCUMENT_DEFAULTS.AUTHOR
      }
      // Create stream to disk
      pdfDoc.pipe(fs.createWriteStream(outputFilename))
      // Add documents to file 
      documents.forEach(doc => {
        pdfDoc.addPage()
          .text(doc.name, DOCUMENT_DEFAULTS.MARGIN_LEFT, DOCUMENT_DEFAULTS.MARGIN_TOP)
          .image(doc.filename, DOCUMENT_DEFAULTS.MARGIN_LEFT, DOCUMENT_DEFAULTS.MARGIN_TOP * 2, { fit: [DOCUMENT_DEFAULTS.IMG_WIDTH, 792] })
      })

      pdfDoc.end()
// ...

Find attached the original image and the generated pdf

@virusakos
Copy link

I am also having this issue.
I am adding images in a PDF and they are rotated.

When I open the image on my PC it is showing in Portrait.
When I am adding it in a PDF it's rotated 90 degrees counterclockwise.

@virusakos
Copy link

I have solved the issue with the rotated images outside of pdfkit.
I am only dealing with JPEGs so I have used https://github.com/johansatge/jpeg-autorotate to fix the rotation of images before adding them to the PDF.

@jayesh-solutelabs
Copy link

@virusakos is there any permanent solution for this issue ? ..because we have dynamic image so it can be of multiple type(image/*)

@virusakos
Copy link

@JayeshLadumor97 I am sorry but I don't know.

@hendricius
Copy link

We are still having the issue. I just investigated and I think this is related to EXIF information in the images.

Image that gets rotated in pdfkit:

rotate-image

Image that does not get rotated:

testimg

I didn't have a look at the source code yet, are we using pdf.js internally? If so - it might be fixed with this release here: mozilla/pdf.js#12027

@razr22
Copy link

razr22 commented Mar 1, 2022

any fix for this ?

@nico1996it
Copy link

i think we really have to fix this bug

@m1daz
Copy link

m1daz commented Jun 19, 2024

Hello I see this is still open, is there any fix for this or is it still being worked on? Thank you!

@blikblum
Copy link
Member

Should be fixed. See https://github.com/foliojs/pdfkit/blob/master/docs/images.md ignoreOrientation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants