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

Add image without distorting ratio #3401

Closed
mednche opened this issue Apr 1, 2022 · 6 comments
Closed

Add image without distorting ratio #3401

mednche opened this issue Apr 1, 2022 · 6 comments

Comments

@mednche
Copy link

mednche commented Apr 1, 2022

"I have read and understood the contribution guidelines."

I am trying to add an image (dataURL format) to my PDF to fit in a 65*60 mm box.

My current code is:

doc.addImage(imageURL, "JPEG", config_settings.x, config_settings.y, 65, 60, "", "FAST", 0);

However I get a distorted image as below, which doesn't respect the aspect ratio of the original image.

Screenshot 2022-04-01 at 10 17 22

My images are already in the correct dimensions to fit in the box (see below for the correct ratio) so I'd like to be able to add them to the PDF without enforcing a width and height.

62456082f93f8832b2dac0f2

Alternatively, is there a way to specify a resize mode for my image (e.g. 'contain')?

@JobSoltero
Copy link

JobSoltero commented Apr 2, 2022

Hi @mednche, I haven't seen this kind of functionality in jsPDF but in cases like yours I use this formula, using html2canvas:

...
      html2canvas(input)
          .then((canvas) => {
            const imgData = canvas.toDataURL('image/jpeg'); 
            const pdf = new jsPDF();
            const imgProps= pdf.getImageProperties(imgData);
            const width = 65 //which is your container width
            const height = (imgProps.height * width) / imgProps.width;
            pdf.addImage(imgData, "JPEG", config_settings.x, config_settings.y, width, height);
            pdf.save('download.pdf');
      });
        ...

Also make sure u are using the correct units.

@mednche
Copy link
Author

mednche commented Apr 3, 2022

Thanks for your reply @JobSoltero! Can you explain a bit more what html2canvas() does in this case (I was under the impression that it was typically used to take a screenshot of the current screen) and also what input would be in my case? Thanks!

@JobSoltero
Copy link

JobSoltero commented Apr 3, 2022

Yeah it uses an html as input and then converts it into an Image. For your case u can directly use the Image url

const pdf = new jsPDF(); 
const imgProps = pdf.getImageProperties(imgUrl);
const width = 65 //which is your container width 
const height = (imgProps.height * width) / imgProps.width; 
pdf.addImage(imgUrl, "JPEG", config_settings.x, config_settings.y, width, height); pdf.save('download.pdf'); 

@JobSoltero
Copy link

@mednche Do you have any update in this bug?

@mednche
Copy link
Author

mednche commented Apr 10, 2022

Thanks for your answer @JobSoltero! It helped me find a solution. I didn't know about pdf.getImageProperties() - it did the trick!

@JobSoltero
Copy link

Great!, I guess this issue can be closed now

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

3 participants