HTML to PDF?

I am trying to convert an html element from my UI into a PDF.

Tried out some different libs:

They all seem to fail to render it completely, i think the shadow dom is ignored and some css imports also fail.

Is there a workaround for this?

If you “print” the html how different is is from what you get rendered to PDF? Depending on your automation needs you might want to try “printing” to a PDF. That is save the HTML as a file and then render the HTML to a PDF via a utility program.

There are libraries both JavaScript and Java for HTML to PDF conversion. However almost all of them tend to have a common problem not supporting web components. So they fail pretty easily when you use case is to convert something else than native HTML to PDF.

This usually means that for generating PDF reports you need to build a separate process and not to convert actual UI to PDF. Instead produce separate report file which native HTML and convert that to PDF. Alternatively you could skip the HTML totally by using some library like Apache POI to to produce the report. There are also many fully report generation oriented libraries in Java ecosystem.

Hey, yes sadly thats not an option for me, I need the HTML one-to-one to PDF.

Then you need something that is really capable of rendering web component. This pretty much means running Browser command line with shell script. You can do this e.g. with Chrome Headless Chrome to print pdf - Stack Overflow This means that you need to have Chrome and possibly some other software installed in your server. You can execute external command from Java.

Yeah, only problem with that is that its not really performant, since you create a new browser, and I can see this being an issue if there are many concurrent conversions.

For anyone wondering, I created a fork and added support for shadow-root stuff. Only issue with that currently is that vaadin values somehow render incorrectly, the rest works fine though: GitHub - Osiris-Team/dom-to-image-2: Generates an image from a DOM node using HTML5 canvas

Then using pdflib you can create a pdf from that image.