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

PDFTable Image Quality #109

Closed
yhelfronda opened this issue Jan 19, 2019 · 11 comments
Closed

PDFTable Image Quality #109

yhelfronda opened this issue Jan 19, 2019 · 11 comments

Comments

@yhelfronda
Copy link

Hello,

Thank you for this wonderful library. This is so helpful.

I just want to ask if would it be possible to control the image quality on the table? 'Cause right now it kinda blurry. I would like to remain the quality of the image as possible so it won't appear blurry. Thank you in advance for your help.

@philprime
Copy link
Member

Hi @yhelfronda, can you please post the code you are using to create the PDFImage?

Usually you can define the quality for each image object

@yhelfronda
Copy link
Author

yhelfronda commented Jan 25, 2019

Hello @techprimate ,

First, thank you very much on looking into my issue. (y)

I'm adding an image on the table but it seems that PDFImage is not supported, so i used UIImage instead. There's no problem with the image quality like the code below:

document.addImage(.contentCenter, image: PDFImage(image:newChartImage, caption: PDFAttributedText(text: NSAttributedString(string: "ACCIDENTS CAUSE", attributes: titleAttributes)), sizeFit: PDFImageSizeFit.widthHeight, quality: 1.0, options: PDFImageOptions.resize))

But if i used this code inside the PDFTable cell/content it doesn't help:

try table.generateCells( data: [PDFImage(image: newChartImage, sizeFit: PDFImageSizeFit.widthHeight, quality: 1.0, options: PDFImageOptions.resize).image], alignments: [.center , .topLeft] )

image
The quality of the image is blurry. Am I missing something or what? I need your help. Thank you very much in advance!

@philprime
Copy link
Member

I don't think you are missing something, if you use an UIImage as the table cell content, as intended, it will create a PDFImage inside the renderer (All elements of an PDFTable are converted into other elements).

The default quality of an PDFImage is set to 85% for no specific reason (always felt like a good value).
You could try to change line 370 in PDFTableObject.swift and add a image quality of 1.0 to the PDFImage initialiser.

contentObject = PDFImageObject(image: PDFImage(image: contentImage))

If this resolves the problem, it is definitely the default image quality and we need to find a better solution such as using PDFImage as table content.
If it does not, we need more investigations to find the reason behind the quality lost.

@yhelfronda
Copy link
Author

Hello @techprimate-phil ,

I tried your suggestion, but still didn't work.

if let contentImage = content?.imageValue { contentObject = PDFImageObject(image: PDFImage(image: contentImage, quality: 1.0)) }

I tried to add options: .resize as well but still the same. And also, to know if the quality parameter works, I tried to set it to 0.0, but the quality of the image is still the same as the default.

Hopefully, this will resolved soon. Thank you very much!

@philprime
Copy link
Member

is it possible for you to provide us the image so we can debug this exact case?

@yhelfronda
Copy link
Author

yhelfronda commented Feb 2, 2019

Hello @techprimate-phil,

Here's an actual image that has been used on the generated pdf. By the way, the width of the table on A4 paper size is:

table.widths = [ 0.3 , 0.7 ], where 0.3 is the image's container.

1548203065_picture

Thank you for your help!

@yhelfronda
Copy link
Author

yhelfronda commented Feb 28, 2019

Hello @techprimate-phil,

I think I found the issue here. The code below from PDFImageObject.swift causes the image to be blurry even you set the image quality and options.

override func draw(generator: PDFGenerator, container: PDFContainer) throws { PDFGraphics.resizeAndCompressImage(image: image.image, frame: frame, shouldResize: image.options.contains(.resize), shouldCompress: image.options.contains(.compress), quality: image.quality).draw(in: self.frame) }

image.options.contains(.resize) is always set to true that's why it keeps on resizing the image losing the image quality.

As a solution, I modified your code and added a new image compression .none in PDFImageOptions.swift to ignore the condition inside this function resizeAndCompressImage. Then I change the code in line 370 to this:

contentObject = PDFImageObject(image: PDFImage(image: contentImage, quality: 1.0, options: [.none]))

And the problem solved! 👍 👏 👏 👏 🎉 🎊

Thank you!

@philprime
Copy link
Member

Oh, that's great news!
I am sorry I didn't get back to your issue earlier, so much going on right now.

Would you mind to create a Pull Request with your changes?

@yhelfronda
Copy link
Author

Hello @techprimate-phil,

It's okay. No worries.

BTW, I just created a pull request.

And I just realised that the fix won't allow you to control the image quality inside the table because the value for option has been permanently set to .none.

I would be great if we would be able to control the value of the option parameter when adding an image into the table. Instead of adding/populating a UIImage in the table, use a PDFImage object instead, so we have the full control of the image quality, where we can set the image's quality and compression. This is just my suggestion.

Thank you again for this such a great library.

@philprime
Copy link
Member

Thank you again for your contribution. I merged your pull request and will release 1.3.3 now

@yhelfronda
Copy link
Author

You're welcome! Thank you again for this wonderful library.

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