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.

Add image without distorting ratio

See original GitHub issue

“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’)?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
JobSolterocommented, Apr 4, 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'); 
1reaction
mednchecommented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Steps to Resize Images Without Distortion 2022
Step 1: Launch Photoshop · Step 2: Open the Aspect Ratio Window · Step 3: Set the Aspect Ratio · Step 4: Set...
Read more >
Resize image without distorting it - css - Stack Overflow
The issue is with setting width to 100%. Try removing width="100%", or set the width manually based on the aspect ratio. Share.
Read more >
How to fill a box with an image without distorting it
The image should completely fill the box, retaining aspect ratio, and cropping any excess on the side that is too big to fit....
Read more >
Resizing Images in Office without distorting (Word, Excel, and ...
To avoid distortion, just drag using SHIFT + CORNER HANDLE–(No need to even check if the image is proportionally locked):.
Read more >
How to Resize an Image without Distorting it | Phancybox NZ
You need to crop the image first. Cropping an image is essentially cutting out a portion of it, which will alter it's ratio....
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