-
Notifications
You must be signed in to change notification settings - Fork 357
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
Image is broken if interpolate is set to false in generated pdf #358
Comments
I will look into. @rototor might also have an idea. In the meantime things to try: Also, please use the fast renderer |
The embedded image with interpolation=false misses the DecodeParams dictionary entry in the image (screenshot of the files in the PDFDebugger): Because of this the image is of course broken, as it can not be decoded correctly. The full html source of the broken case would be perfect to investigate that problem. Beside this: Don't use images for barcodes. Interpolate is not respected by most PDF viewers / printers... You have two ways fix that problem: Export the barcode as SVG and embed it as a vector SVG image. Or register a barcode object. I.e.
inside the HTML:
This way you get the bars as vector fills and they will always be fine and not blurry with every printer / pdf viewer. Don't use iText for anything, iText 2 is way outdated and I am pretty sure that you don't want to pay the premium you need for a iText 5 license. Just use barcode4j for this. This is some code I use for a EAN128 barcode:
You then only need to draw the SVG in the object drawer. Or you just inline the SVG. Which may likely be the easiest way. |
Thank you for your responses @rototor @danfickle.
|
@nikycube To be honest I use some legacy lib here (jasperreports-3.7.4) to render the barcode on the canvas. i.e.
Doing this the right way needs some more code as you need to transcode the image using batik, see the classes in https://github.com/danfickle/openhtmltopdf/tree/open-dev-v1/openhtmltopdf-svg-support/src/main/java/com/openhtmltopdf/svgsupport Stupid question: You did register the SVG drawer in the builder? |
Yes, I did register the SVG drawer in the builder. If you are referring on my first approach. The thing is that
I would rather take first approach with SVG embed it, if it works. Thank you! |
Hi @nikycube, as a quick suggestion, try getting rid of the |
We can now use unit values such as mm in the width/height attribute of SVG images. With tests.
Another issue you will find is that the width/height attributes only accept unit less values. So use css instead to size the svg element. |
Hi again related to rendering svg image I got rid of |
Seems to work for me! I used this builder code: try (OutputStream os = new FileOutputStream("/Users/me/Documents/pdf-issues/output/issue-358.pdf")) {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withUri("file:///Users/me/Documents/pdf-issues/issue-358.htm");
builder.toStream(os);
builder.useSVGDrawer(new BatikSVGDrawer());
builder.useFastMode();
builder.run();
} and this html file: <html lang="en"> <head>
<style>
div.header { position: running(header); }
div.footer { position: running(footer); }
@page { @top-center { content: element(header) }
@bottom-center { content: element(footer) };
margin-bottom: 3.3cm;
}
#pagenumber:before { content: counter(page) }
#pagecount:before { content: counter(pages) }
</style> </head>
<body>
<div class="header">ss</div> <div class="footer"><span id="pagenumber" ></span></div> <div class="content"><svg xmlns="http://www.w3.org/2000/svg" height="8.7637mm" viewBox="0 0 32.55 8.7637" width="32.55mm"><g fill="black" stroke="none"><rect height="7" width="0.21" x="2.1" y="0"/><rect height="7" width="0.21" x="2.52" y="0"/><rect height="7" width="0.63" x="2.94" y="0"/><rect height="7" width="0.21" x="4.2" y="0"/><rect height="7" width="0.21" x="4.62" y="0"/><rect height="7" width="0.63" x="5.04" y="0"/><rect height="7" width="0.21" x="5.88" y="0"/><rect height="7" width="0.21" x="6.72" y="0"/><rect height="7" width="0.63" x="7.14" y="0"/><rect height="7" width="0.63" x="7.98" y="0"/><rect height="7" width="0.21" x="8.82" y="0"/><rect height="7" width="0.21" x="9.66" y="0"/><rect height="7" width="0.63" x="10.5" y="0"/><rect height="7" width="0.21" x="11.34" y="0"/><rect height="7" width="0.63" x="11.76" y="0"/><rect height="7" width="0.21" x="13.02" y="0"/><rect height="7" width="0.21" x="13.86" y="0"/><rect height="7" width="0.21" x="14.28" y="0"/><rect height="7" width="0.21" x="15.12" y="0"/><rect height="7" width="0.63" x="15.54" y="0"/><rect height="7" width="0.63" x="16.8" y="0"/><rect height="7" width="0.21" x="17.64" y="0"/><rect height="7" width="0.63" x="18.06" y="0"/><rect height="7" width="0.21" x="19.32" y="0"/><rect height="7" width="0.21" x="20.16" y="0"/><rect height="7" width="0.21" x="20.58" y="0"/><rect height="7" width="0.63" x="21" y="0"/><rect height="7" width="0.21" x="21.84" y="0"/><rect height="7" width="0.21" x="22.68" y="0"/><rect height="7" width="0.63" x="23.52" y="0"/><rect height="7" width="0.63" x="24.36" y="0"/><rect height="7" width="0.21" x="25.2" y="0"/><rect height="7" width="0.63" x="25.62" y="0"/><rect height="7" width="0.63" x="26.46" y="0"/><rect height="7" width="0.21" x="27.3" y="0"/><rect height="7" width="0.21" x="27.72" y="0"/><rect height="7" width="0.21" x="28.56" y="0"/><rect height="7" width="0.63" x="29.4" y="0"/><rect height="7" width="0.21" x="30.24" y="0"/><text font-family="Helvetica" font-size="1.7637" text-anchor="middle" x="16.275" y="8.6392">81675005130337</text></g></svg></div> </body> </html> Note the Finally, this was the resulting PDF in the default branch: and this was the result in the The only other thing I can think of is that replaced elements such as SVG must have |
Thank you! Finally I succeeded to render pdf with svg. I added in style |
…mg tag. This is mostly a revert of d9cdd6a I forgot about external SVGs rather than inline SVGs where the width/height attribute are converted to CSS earlier in the render process.
I create my barcode in memory, and my server provider a url to this barcode image, then just use |
Hi @danfickle I'm trying to render a pdf which contains images more exactly some barcodes.
Initially I tried to render with interpolate set to true and I got blurry image. See (Picture_with_interpolate_true.pdf)
After I did some research I found your post in which you recommended to set interpolate to be false (image-rendering: pixelated;) I entered in debug mode and I saw that in PdfBoxSlowOutputDevice.class:712 PDImageXObject is set to true.
As result the image is broken. See (Picture_with_pixelated.pdf)
I have attached two pdfs with both cases.
I used version: '0.0.1-RC20' from 'openhtmltopdf-pdfbox'.
The image was created with code bellow :
Thank you!
Picture_with_interpolate_true.pdf
Picture_with_pixelated.pdf
The text was updated successfully, but these errors were encountered: