Skip to content
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

exif orientation is handled incorrectly #3795

Open
Fuzzyma opened this issue Nov 21, 2024 · 6 comments
Open

exif orientation is handled incorrectly #3795

Fuzzyma opened this issue Nov 21, 2024 · 6 comments

Comments

@Fuzzyma
Copy link

Fuzzyma commented Nov 21, 2024

jspdf handles exif orientation and correctly flips width and height. However, it does not actually rotate the image. Instead it just stretches the unrotated image into its rotated shape.

This issue has been rised twice already but it seems that nobody realized that the image is not correctly rotated

In both issues you can actually see, that albeit the image dimension was rotated, the content was not.
I encountered the issue when using svg2pdf.js. You can also see another example in the issue I raised: yWorks/svg2pdf.js#315. The orientation is correct, the content is still unrotated and just stretched

@NikolaStojicic
Copy link

I am facing same issue with addImage function, it does not respect exif orientation.

Bellow you can find my suboptimal solution if anyone needs it before this gets addressed.

Uses canvas to draw image first, canvas rendering will display image correctly then grabbing data and passing it to addImage fn.

import jsPDF from 'jspdf';

async function createImage(src: string) {
    const img = new Image();
    img.src = src;
    img.setAttribute('crossorigin', 'anonymous');

    return new Promise<{
      width: number;
      height: number;
      imageCanvasData: string;
    }>((resolve) => {
      img.onload = () => {
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = img.width;
        canvas.height = img.height;
        ctx?.drawImage(img, 0, 0, img.width, img.height);
        const imageCanvasData = canvas.toDataURL('image/jpeg');
        canvas.remove();
        resolve({ width: img.width, height: img.height, imageCanvasData });
      };

      img.onerror = () => resolve({ width: 0, height: 0, imageCanvasData: '' });
    });
  }

  const pdf = new jsPDF();

  const { width, height, imageCanvasData } = await createImage(
    'https://images.unsplash.com/photo-1504805572947-34fad45aed93?q=20&w=200',
  );

  console.log(width, height, imageCanvasData);
  pdf.addImage(imageCanvasData, 'JPEG', 0, 0, width, height);
  pdf.save('download.pdf');

@mbrunneritlabs
Copy link

mbrunneritlabs commented Dec 13, 2024

I also think something is wrong with how jspdf handles the rotation.

Lets say i have an image as Base64 with the following dimensions:

width: 100
height: 50
rotation: 90*

After creating an image

const img = new Image()
img.src("Base64String")

Then the dimensions seem correctly rotated because

imageWidth = img.width //= 50
imageHeight= img.height //= 100

Then i add an page to jspdf with

      doc.addPage([imageWidth, imageHeight]);

and add the correct image like this

doc.addImage(img, 'JPEG', 0, 0, imageWidth, imageHeight, undefined, 'MEDIUM');

On my pdf then the page dimensions are correct (height is bigger then width), but the image is not rotated even tough it most probably was already rotated when creating the image .

The workaround with drawing on Canvas and giving this image to jspdf works.

@Fuzzyma
Copy link
Author

Fuzzyma commented Feb 12, 2025

@HackbrettXXX if you point me to the approximate position of where the code for this functionality is located, I am happy to create a PR (when I find the time lol)

@HackbrettXXX
Copy link
Collaborator

Maybe around here?

var determineWidthAndHeight = function(width, height, image) {

You might use @MrRio's feat/typescript branch as base. Not sure in what state that is, though.

@Fuzzyma
Copy link
Author

Fuzzyma commented Feb 12, 2025

@HackbrettXXX maybe we should get that over the line first then? Seems like a major rework to add typescript

@MrRio
Copy link
Member

MrRio commented Feb 12, 2025

I wouldn't worry too much about the TypeScript branch for now. Going to take an approach which involves a bunch of smart automation, so feel free to get this merged when it's ready.

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

No branches or pull requests

5 participants