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

after adding Qr Image, other image background became green. #89

Closed
jaypeevns opened this issue Apr 1, 2019 · 11 comments
Closed

after adding Qr Image, other image background became green. #89

jaypeevns opened this issue Apr 1, 2019 · 11 comments

Comments

@jaypeevns
Copy link

Hi Andrew,

I am having a pdf in which we already have three images. Now I need to attach a QR code in it. when I am doing it pragmatically , I am getting green background on result pdf.

I am attaching before and after snapshot of pdf.

Please help us to resolve this issue.

Regards,
jay Prakash Verma
beforeQREmbed
afterQrEmbed

@Hopding
Copy link
Owner

Hopding commented Apr 1, 2019

@jaypeevns Can you please share the code you are using to add the QR Code? Also, if you're able to provide the original PDF, that would help me to reproduce the issue.

@jaypeevns
Copy link
Author

Hi Andrew,

I can provide you the code but I can't give you pdf. in the pdf we are having 3 images and we are adding one qr code png in it.

var { PDFDocumentFactory, PDFDocumentWriter, drawImage} = require('pdf-lib');
var fs = require('fs');
var qr_pdf_bytes ={};
qr_pdf_bytes.pdfBytes = fs.readFileSync("location of file pdf");
qr_pdf_bytes.qrBytes= fs.readFileSync("location of QR code png");
var QRCode_PNG = 'QRCodePng';
var pdfDoc = PDFDocumentFactory.load(qr_pdf_bytes.pdfBytes);
var [QRCodePngRef, QRCodePngDims] = pdfDoc.embedPNG(qr_pdf_bytes.qrBytes);
var pages = pdfDoc.getPages();
var existingPage = pages[0]
.addImageObject(QRCode_PNG, QRCodePngRef);
var newContentStream = pdfDoc.createContentStream(
drawImage(QRCode_PNG, {
x: parseInt("55"),
y: parseInt("65"),
width: 50,
height: 50,
})
);
existingPage.addContentStreams(pdfDoc.register(newContentStream));
qr_pdf_bytes.pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);

@ghost
Copy link

ghost commented Apr 2, 2019

I've had a very similar issue, but in my case the problem seems to derive from the compression level of a pdf file.
Compression example

If you have a compressed pdf, some illustrations become very glitchy and odd. Whereas uncompressed pdf's turn out just fine.
Without manipulating the pdf's, just loading and unloading them, this seems to occur every time.

I've personally resolved the issue by avoiding pdf compression.

@Hopding
Copy link
Owner

Hopding commented Apr 2, 2019

@Crunchyalex Nearly all PDF documents contain compressed data. And thus far, I've been unable to produce a similar result on any PDF I've tested. Can you please clarify what you mean by "compressed pdf"?

Also, would you happen to have a sample PDF I can use to reproduce the issue? I don't think I'll be able to resolve this issue very quickly without access to a PDF I can test on. Or, alternatively, a way that I can generate such a PDF.

@jaypeevns
Copy link
Author

Hi Andrew,

I am attaching the template where we are facing issue. please attach any qr image on it and help me if you are able to replicate the issue.
beforeQREmbed.pdf

@jaypeevns
Copy link
Author

Hi andrew,

Please let me know if you are able to replicate the issue.

@Hopding
Copy link
Owner

Hopding commented Apr 10, 2019

Hello @jaypeevns. I've been swamped with work and school lately. I'm afraid I haven't had a chance to look any further into this yet. I'll let you know as soon as I do. Hopefully within the next two weeks.

@jaypeevns
Copy link
Author

Thanks Andrew, I am eagerly waiting your reply.

@Hopding
Copy link
Owner

Hopding commented Apr 28, 2019

Hello @jaypeevns! I finally got some time to investigate this today.

The cause of this issue was several invalid strings being produced by pdf-lib. Basically, pdf-lib was escaping \, (, and ) characters in PDFString objects when it should not have been.

Anyways, I was able to fix this in #100. With these changes, everything works smoothly, and the three images in your PDF no longer have green backgrounds.

I just cut prerelease 0.6.2-rc2 with the fix.

You can install this prerelease with npm:

npm install pdf-lib@0.6.2-rc2

It's also available on unpkg:

Please try it out and let me know if it works for you!

@Crunchyalex I'd be grateful if you'd test this out as well.

@ghost
Copy link

ghost commented Apr 30, 2019

I've tested it with the exact same pdf as before, and it doesn't produce the same artifact on your prerelease build!
Thank you for resolving this.
And for keeping this library up and healthy! It's very useful!

@Hopding
Copy link
Owner

Hopding commented May 5, 2019

Version 0.6.2 is now published. It contains fix for this issue. The full release notes are available here.

You can install this new version with npm:

npm install pdf-lib@0.6.2

It's also available on unpkg:

@Hopding Hopding closed this as completed May 5, 2019
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

2 participants