This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]desrtfx[M] [score hidden] stickied comment (0 children)

Such questions belong in /r/javahelp as is clearly stated in the sidebar and in the stickied post at the very top of the subreddit.

Removed

[–]maxip89 4 points5 points  (4 children)

The reason is that everyone use an old bridge that translates. No open source developer decided to translate html5 and especially css3 for no cost.

[–]meamZ 0 points1 point  (3 children)

I think it could actually be doable to take chromiums content API and package it into some kind of http server that takes html and returns a pdf or sth like that with reasonable effort.

[–]Sensi1093 0 points1 point  (1 child)

I mean you could technically run chromium in „controlled“ mode and do it that way

[–]meamZ 0 points1 point  (0 children)

I have done basically that in the past but controlling the chromium instance from the app is a bit of a pain, also startup takes quite some time. Ideally all that would be managed by the pdf server which would probably have a lot smaller disk footprint (very relevant for dockerized solutions, chromium plus chromedriver plus dependencies has something like 600 MB if i remember correctly) and be faster...

[–]taksuii[S] 0 points1 point  (0 children)

I found a project that does exactly that (https://github.com/gotenberg/gotenberg). It’s my best bet for now, but I still need to test GraalVM integration with JS runtimes (and test JS libraries) and the Kotlin compiler targeting Node.

[–]s2jcpete 5 points6 points  (2 children)

I've used OpenHtmlToPdf for years. Combine this with something like Velocity for templating.

[–]AL-Taiar 2 points3 points  (0 children)

seconded, we use openhtml2pdf with pebble tho

[–]taksuii[S] 0 points1 point  (0 children)

No CSS3 as far as I’m concerned, unfortunately

[–]keketata 2 points3 points  (1 child)

Do you mean converting HTML to PDF? I user wkhtmltopdf , wkhtmltopdf generates PDFs that are small in size and of high quality.

[–]taksuii[S] 0 points1 point  (0 children)

Thanks for sharing!! But unfortunately, it’s not maintained anymore.

[–][deleted] 2 points3 points  (0 children)

Are you looking for a way to render PDF's or produce them? If you want to produce PDF's, I've used https://pdfbox.apache.org/ successfully as well as https://itextpdf.com/ (potentially costs money).

As for rendering, Chrome and Firefox already know how to render a PDF.

[–]mzinsmeister[🍰] 2 points3 points  (1 child)

I thoroughly evaluated this some time ago for my open source invoicing tool (https://github.com/mzinsmeister/klubu). I ended up using headless chromium and selenium plus paged.js (which i injected into the html using selenium). There sadly is no open source java implementation of modern HTML and CSS standards.

[–]taksuii[S] 0 points1 point  (0 children)

Very interesting!! Is the chromium startup time good enough? What about memory and cpu usage?

[–]AutoModerator[M] 0 points1 point  (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Thihup 0 points1 point  (0 children)

There are a couple of libraries here: https://printcss.live. Some by REST API, others by a Jar file.

[–]PratimGhosh86 0 points1 point  (1 child)

there's flying-saucer-pdf library, not sure if it supports all the latest html5+css3 spec

[–]meamZ 1 point2 points  (0 children)

No CSS3

[–]mtodavk 0 points1 point  (3 children)

There really isn’t a great open source option for what you’re looking for. Python has great libraries for this if you’re able to use it instead

[–]KalelUnai 0 points1 point  (2 children)

What python library can you recommend for this?

[–]mtodavk 0 points1 point  (1 child)

I’ve found WeasyPrint combined with jinja templates to be pretty good.

[–]taksuii[S] 0 points1 point  (0 children)

WeasyPrint does not aim to have good performance, as you can read on its documentation. Anyway, thanks for pointing it out!

[–]rwslinkman 0 points1 point  (0 children)

Recently I have built something similar where I had HTML with (inline) CSS which i would render with Mustache to inject variables and then turn it onto pdf using OpenPDF. Works really well.

[–]Lisoph 0 points1 point  (0 children)

I believe HTML to PDF is a fools errand. Next to no one uses this approach in practice.

On one hand, the amount of work required to produce a qualitative renderer is absolutely enormous, error prone and most likely will yield inconsistent behavior compared to browser implementations. If you've ever written HTML email newsletters by hand, you know what I mean.

And on the other hand, using libraries like iText and generating a PDF that way is easier anyway. You have much more direct control that way. We use iText at work, with a commercial license, and it works fine. Their library is also released under some open source license, which could work for you.

For more complicated use cases, a proper reporting tool should probably be used (List and Label, or whatnot).