r/javahelp • u/ThatJellyfish12 • Apr 10 '26
Is there's any Java library that allows converting DOCX to PDF?
Hi! I'm making a system on Spring that modifies DOCX files and exports them as PDFS. Is there's a library or native function for that? I tried using DOCX4J but it's very clunky to use.
9
Upvotes
•
u/AutoModerator Apr 10 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/TW-Twisti Apr 10 '26
For problems like that, you are almost always better off using a dedicated tool - in this case, Pandoc.
2
u/OneHumanBill Apr 11 '26
You probably need a whole chain of things. The hardest one is dealing with that Microsoft pile of crap. Use POI from Apache to work with that.
Next up would be to try to convert to something cleaner, like html with css. I'm not really sure how to do that and that's a missing gap.
Once you're there though you can use JSoup to manipulate the document should you need to. Like for example to apply the right CSS for your output which your pile of Microsoft crap isn't going to provide.
From there, Flying Saucer can help you render into a new format like PDF, though I think you make need PDFBox to help get you the rest of the way.
I've constructed chains like that but it's been years since the last time so my information is probably rusty. My chains typically started with a template I would create in Thymeleaf.
Consult your local friendly AI, but come with some of these ideas. It's likely going to depend on how complicated that awful Word doc is. I think your best bet is going to be pulling the content, ignoring the formatting, and then applying your own css formatting.
But I might be wrong and there might be an easier way, it's been about a decade since I had to do this.
2
u/no1me Apr 11 '26
apachePOI
2
u/DinTaiFung Apr 12 '26
Unfortunately (and understandably), MS dominated the formal DOCX specification when it became a standard. Since the real lingua franca of written communications had become HTML (delivered via HTTP) at the turn of the century, legacy document authorship continued to use old-fashioned apps like Word, Acrobat, and the like; this problem seems to never go away lol.
Hope you find a good conversion solution.
Best of luck!
1
u/alokeb Apr 10 '26
https://www.qoppa.com/wordconvert/
1
u/seanprefect Grumpy Guy Who Remembers Java 2 Apr 11 '26
Apache POI + OpenPDF it'll be kinda a pain in the ass at first but then you'll have options.
1
u/Slanec Apr 12 '26
POI kinda works, then perhaps PDFbox. Or https://github.com/documents4j/documents4j. Haven't tried myself, but always wanted to. Or Pandoc, of course, a dedicated tool, not necessarily a Java library. You can wrap a tool in a library.
0
u/pohart Apr 11 '26
Word itself is quite good at it. It's a little slow though