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.

addImage with compression repeats same image

See original GitHub issue

So… This is pretty weird. I have a function that crops an image in different pieces and adds them to different pages of my pdf document. However, when I enable compression in the addimage function, the first image is repeated.

Code:

html2canvas(srcElWrapper, {
		background:'#fff',
		onrendered: function(canvas) {
			
			var image = new Image();
			image.src = canvas.toDataURL("image/jpeg");
			
			orient = 'p';
				
			// TEST
			l = {
				orientation: orient,
				unit: 'mm',
				format: 'a4',
				compress: true,
				fontSize: 8,
				lineHeight: 1,
				autoSize: false,
				printHeaders: true
			};
			
			var doc = new jsPDF(l);

			widthOfOnePiece = 992;
			heightOfOnePiece = 1403;
			
			numColsToCut = Math.ceil(canvas.width/widthOfOnePiece);
			numRowsToCut = Math.ceil(canvas.height/heightOfOnePiece);

			totalWidth = 0;
			totalHeight = 0;
				for(var x = 0; x < numColsToCut; x++) {
					for(var y = 0; y < numRowsToCut; y++) {
						var canvas = document.createElement('canvas');
						canvas.width = widthOfOnePiece;
						canvas.height = heightOfOnePiece;
						var context = canvas.getContext('2d');
						context.drawImage(image, x * widthOfOnePiece, y * heightOfOnePiece, widthOfOnePiece, heightOfOnePiece, 0, 0, canvas.width, canvas.height);
						
						// THIS OPENS RIGHT PART OF IMAGE:
						window.open(canvas.toDataURL());

						
						if ( x > 0 ) { doc.addPage(); }
						
						// THIS WORKS: doc.addImage(canvas.toDataURL(), 'jpg', 5, 15, 210, 297);
						
						// THIS DOESNT:
						doc.addImage(canvas.toDataURL(), 'jpg', 5, 15, 210, 297,'','FAST');
					}
				}
			
			doc.save('testsaves.pdf');
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

6reactions
sgelbcommented, Feb 27, 2017

This has little to do with compression. Have a look. Despite the compression option, what else did change?

doc.addImage(canvas.toDataURL(), 'jpg', 5, 15, 210, 297);
doc.addImage(canvas.toDataURL(), 'jpg', 5, 15, 210, 297,'','FAST');

You found it? Well, that empty string is setting an alias. The idea is that jsPDF reuses images with known aliases. So of course your first image is repeated again and again, you told jsPDF to do so.

There are basically three options:

  1. Set alias to null or undefined.
  2. Put all arguments in an object, omitting alias and pass this to addImage. It’s basically the same as option 1, but looks better to some.
  3. Set an unique alias for each unique image. I would always do this: if no alias is set, jsPDF will calculate a hash instead, which can easily double or tripple the time it uses for adding an image.
2reactions
adongcommented, Jan 18, 2019

@sgelb You could also use the following to leverage compression without caching repeated images.

pdf.addImage(base64Image, 'png', x, y, width, height, NaN, 'FAST');

NaN is the key here 😃

https://github.com/MrRio/jsPDF/blob/d00a9dfde5fdf4f9f3c5f67a9231391975793c17/docs/modules_addimage.js.html#L339

You know, since NaN === NaN returns false

Read more comments on GitHub >

github_iconTop Results From Across the Web

addImage with compression repeats same image #1036
So.. This is pretty weird. I have a function that crops an image in different pieces and adds them to different pages of...
Read more >
Is there a way to prevent repeating the same image in all ...
I am using JsPdf to generate pdf from multiple images, the issue is that I get the same image generated in ...
Read more >
Working with Image Destinations
Explains how to read and write image data using the Image I/O framework.
Read more >
Does repeated JPEG compression ruin images? - the last word
In Photoshop, JPEG recompressions don't “walk”; they are the same after the first iteration. Changes from the first to the second iteration are ......
Read more >
Class: Image
fillPatternImage, Image, <optional> ... can be "repeat", "repeat-x", "repeat-y", or "no-repeat". ... Image instance layer.add(image); layer.draw(); }); ...
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