Has anyone converted docx to pdf using javascript?

I’m trying to convert DOCX files to PDF using JavaScript for a small upload form, but the tables keep shifting. Has anyone managed this while keeping the original layout intact?

JavaScript in the browser usually won’t preserve DOCX layout exactly, especially tables. I’d upload the file to the server and have Word or LibreOffice export it to PDF, with the same fonts installed and page size set correctly. I learned the hard way that missing fonts alone can shift every table.

Tables shifting is the classic docx-to-pdf pain, browsers are terrible at this. I’d skip doing it in JS entirely and run LibreOffice headless on the server instead, it handles tables way better than anything in the browser. Worth a try before you lose a weekend to it. Is this running on a server or purely client side?

Are you converting in the browser or on a server? Browser-only DOCX conversion is usually where tables drift, because DOCX layout depends on Word’s rendering rules. For upload forms, I’d use PDFcap after upload to convert and check the result, especially if the PDF has to meet an official requirement.

The shifting is expected: DOCX tables re-flow depending on the renderer, and browser-side JavaScript libraries rebuild the layout rather than reproduce it, so widths and page breaks drift. The reliable route is server-side conversion through Word’s own engine or LibreOffice headless, with the original fonts installed. Since this is tagged accessibility, also check the PDF’s table tags and reading order afterwards, as converters often mark tables up badly.

For layout-sensitive files, I would upload the DOCX and convert it server-side with headless LibreOffice, rather than drawing the document in browser JavaScript. Make sure the same fonts used in the DOCX are installed on the conversion machine, since missing fonts are a common cause of shifted tables. I learned this the hard way with patient forms: test merged cells, page breaks, and headers separately before accepting the PDF as final.

DOCX to PDF conversion in JavaScript is tricky because it needs a rendering engine, not just a library. I’ve had better luck uploading the DOCX to a server and converting it with LibreOffice headless before sending the PDF back.

Are you running this server side with something like LibreOffice headless or a node library, or purely client side in the browser? Table shifts usually come down to the conversion engine not handling merged cells or fixed column widths the same way Word does.

If it’s server side, try exporting straight from LibreOffice (soffice --headless --convert-to pdf) first to see if the issue is your JS wrapper or the engine itself. Keep a copy of the original docx before testing, some libraries overwrite source formatting on conversion.

@mysterious_silver Yeah, testing straight from the command line first is the right call, that’s what I do when a job comes back mangled. If soffice gives you a clean PDF, you know the wrapper is the problem, not the engine. Not sure if paths behave the same on Mac.

I deal with this constantly on new hire forms. The simplest thing to check is whether your Word tables have fixed column widths instead of auto-fit, which I learned the hard way breaks in JS renderers. If that still shifts things, sending it to a backend service usually preserves table layouts way better than browser-based scripts.

I see messy tables on client invoices at least 3 times a week because client-side JavaScript libraries are notoriously finicky with layout.

You usually get cleaner results pushing the file to a server-side tool for conversion. What exact library are you using for the upload?

@LostThunder Auto-fit vs fixed widths is the first thing I check when tables jump. I lock the columns in Word before converting any proof I send out, otherwise the PDF never matches. If it still shifts I convert on the server with LibreOffice instead of fighting JS.

@caleb_hill Locking column widths in Word is the same thing I tell our office staff. Saves me from redoing a batch of forms every time. I’ve also had better luck with server-side LibreOffice than any JS route.