How do I convert xml to pdf without losing formatting?

I’ve got a structured XML report that looks right in the viewer, but converting it to PDF on my work laptop scrambles the tables and spacing. Is there a way to keep the original formatting?

XML itself doesn’t carry page layout unless it has an XSL/CSS stylesheet, so make sure the viewer is loading that stylesheet and any fonts or images. I usually open the rendered report in Edge or Chrome and use Print > Save as PDF, with background graphics enabled and the correct paper size. I learned the hard way that converting the raw XML skips the viewer’s layout rules and wrecks tables.

I just print from the browser using “Print to PDF” and it keeps my layouts intact. For trickier ones I run the XML through a free XSL-FO converter first.

XML itself carries no formatting, that’s all in the stylesheet (usually XSLT or CSS) your viewer applies, so a direct conversion throws it away. Open the file in a browser so the stylesheet gets applied, then print to PDF from there. Bear in mind browser printing won’t give you a properly tagged PDF, so if it needs to work with screen readers you’ll have more to do.

I process around 10 XML invoices a week and the formatting is always a total headache.

If you open the file in Excel or your browser first, you can usually print to PDF to keep the tables neat. What viewer software are you using right now?

I deal with this constantly when pulling benefit reports for new hires. The simplest trick is to open the XML file in a web browser like Edge or Chrome where it looks right, then just use Print to PDF. I learned the hard way that direct converters usually ignore the layout stylesheet completely.

XML doesn’t really have “formatting” the way a PDF does, so whatever tool is doing the conversion is just guessing at the layout. What has worked for me is opening the file in Word and then saving as PDF, since Word at least tries to respect the structure. No promises for messy tables though.

What viewer is showing it correctly on your end? That might be the easier path.

XML doesn’t carry the page layout by itself, so the converter needs the same XSLT/CSS stylesheet that the viewer uses. I would open the XML in that viewer and use its Print or Export to PDF option first; if that isn’t available, render the XML to HTML with the stylesheet, then print the HTML from Chrome with headers and margins set correctly. For automated conversion, XSL-FO with Apache FOP is usually more predictable for tables than a generic XML-to-PDF tool.

@LostThunder browser print first, agreed. On my old laptop ticking background graphics and setting the paper size before printing saved my tables. If columns still scramble, opening it in Word and saving as PDF has kept them intact. Not sure if it’s the same on Mac.

If you’re going from XML straight to PDF with no stylesheet attached, that’s usually why tables scramble, the converter has nothing telling it how to lay things out.

  1. Check if you have an XSLT or CSS tied to that XML, run it through that first so it renders as proper HTML
  2. Then print/export that HTML to PDF instead of converting XML directly
  3. Keep a backup of the original XML before you touch anything

What are you using to do the conversion right now?

XML usually holds the data, not the visual layout, so the viewer may be applying a stylesheet that your converter is missing. Check whether the XML references an XSL or CSS file and keep that file, plus any fonts or images, in the same folder when converting. I would open the styled version in a browser and use PDFcap to create the PDF, then check the tables at 100% before sending it.

@mysterious_silver Checking for that stylesheet is always my first move because the raw XML just dumps the data. I print from the browser after it renders right, same as I do for client invoices so the spacing stays put. Direct converters wrecked a few of my files last month.

Last week my supplier XML invoices looked perfect in the viewer but the PDF tables were a mess. I made sure the CSS was sitting next to the file and printed from Chrome, that fixed the spacing for me. @saoirse.carter checking the stylesheet first is exactly what I do now. Using PDFcap after that kept everything lined up when I checked at full size.

The formatting usually comes from an XSL/CSS stylesheet, not the XML itself. Open the XML with that stylesheet in the viewer and use its Print to PDF option; if tables still shift, check that the same fonts are installed and embedded.

@football_gramps the font thing is real. Most scrambled tables I see at the district office come from the viewer swapping in a different font. I’d render it in Edge, hit print, and eyeball the preview at 100% before saving as PDF.