[{"data":1,"prerenderedAt":478},["Reactive",2],{"/guides/html-to-pdf-java":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"dateModified":10,"author":11,"body":12,"_type":473,"_id":474,"_source":475,"_file":476,"_extension":477},"/guides/html-to-pdf-java","guides",false,"","Convert HTML to PDF in Java: What Each Library Costs You","OpenHTMLtoPDF printed our invoice's table header on page 1 only, and iText needs AGPL or a paid licence. PDFShift got it all right in one HttpClient call.","2026-12-03","Cyril Nicodème",{"type":13,"children":14,"toc":465},"root",[15,23,30,35,37,170,176,206,218,231,260,265,273,278,299,305,310,319,324,330,335,344,349,370,376,389,398,403,416,429],{"type":16,"tag":17,"props":18,"children":19},"element","p",{},[20],{"type":21,"value":22},"text","Every way to make a PDF from HTML in Java makes you pay for something. OpenHTMLtoPDF and Flying Saucer don't do flexbox. iText pdfHTML handles flexbox, but it's AGPL or a paid licence. Playwright prints exactly what Chromium prints, and puts a browser in every deploy. PDFShift prints with Chromium too, from one HTTP call with nothing to install. If the PDF is your app's own HTML, it's the one option that keeps your templates and scripts as they are, with no browser to install and no licence to check.",{"type":16,"tag":24,"props":25,"children":27},"h2",{"id":26},"which-java-html-to-pdf-library-should-you-use",[28],{"type":21,"value":29},"Which Java HTML to PDF library should you use?",{"type":16,"tag":17,"props":31,"children":32},{},[33],{"type":21,"value":34},"We sent one invoice with 40 line items through all five on Java 21: a flexbox totals box, a table header set to repeat on every page, and one inline script.",{"type":21,"value":36},"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",{"type":16,"tag":38,"props":39,"children":40},"table",{},[41,65],{"type":16,"tag":42,"props":43,"children":44},"thead",{},[45],{"type":16,"tag":46,"props":47,"children":48},"tr",{},[49,55,60],{"type":16,"tag":50,"props":51,"children":52},"th",{},[53],{"type":21,"value":54},"Route",{"type":16,"tag":50,"props":56,"children":57},{},[58],{"type":21,"value":59},"What it got wrong on our invoice",{"type":16,"tag":50,"props":61,"children":62},{},[63],{"type":21,"value":64},"What it costs you",{"type":16,"tag":66,"props":67,"children":68},"tbody",{},[69,98,116,134,152],{"type":16,"tag":46,"props":70,"children":71},{},[72,82,90],{"type":16,"tag":73,"props":74,"children":75},"td",{},[76],{"type":16,"tag":77,"props":78,"children":79},"strong",{},[80],{"type":21,"value":81},"PDFShift API (Java's HttpClient)",{"type":16,"tag":73,"props":83,"children":84},{},[85],{"type":16,"tag":77,"props":86,"children":87},{},[88],{"type":21,"value":89},"Nothing: totals, header row and script all right",{"type":16,"tag":73,"props":91,"children":92},{},[93],{"type":16,"tag":77,"props":94,"children":95},{},[96],{"type":21,"value":97},"1 credit per PDF, and nothing to install",{"type":16,"tag":46,"props":99,"children":100},{},[101,106,111],{"type":16,"tag":73,"props":102,"children":103},{},[104],{"type":21,"value":105},"Playwright for Java",{"type":16,"tag":73,"props":107,"children":108},{},[109],{"type":21,"value":110},"Nothing",{"type":16,"tag":73,"props":112,"children":113},{},[114],{"type":21,"value":115},"A 220 MB driver jar, plus a browser download",{"type":16,"tag":46,"props":117,"children":118},{},[119,124,129],{"type":16,"tag":73,"props":120,"children":121},{},[122],{"type":21,"value":123},"iText pdfHTML 6.3.4",{"type":16,"tag":73,"props":125,"children":126},{},[127],{"type":21,"value":128},"Didn't run the script",{"type":16,"tag":73,"props":130,"children":131},{},[132],{"type":21,"value":133},"AGPL or a commercial licence",{"type":16,"tag":46,"props":135,"children":136},{},[137,142,147],{"type":16,"tag":73,"props":138,"children":139},{},[140],{"type":21,"value":141},"OpenHTMLtoPDF 1.2.2",{"type":16,"tag":73,"props":143,"children":144},{},[145],{"type":21,"value":146},"Jammed the flexbox totals, header row on page 1 only, no script",{"type":16,"tag":73,"props":148,"children":149},{},[150],{"type":21,"value":151},"No flexbox or grid: a second copy of your layout",{"type":16,"tag":46,"props":153,"children":154},{},[155,160,165],{"type":16,"tag":73,"props":156,"children":157},{},[158],{"type":21,"value":159},"Flying Saucer 10.8.0",{"type":16,"tag":73,"props":161,"children":162},{},[163],{"type":21,"value":164},"Same as OpenHTMLtoPDF, and it threw on our HTML5 until jsoup cleaned it",{"type":16,"tag":73,"props":166,"children":167},{},[168],{"type":21,"value":169},"CSS 2.1, and Java 21 or newer",{"type":16,"tag":24,"props":171,"children":173},{"id":172},"convert-html-to-pdf-in-java-with-openhtmltopdf-and-fix-the-header-row",[174],{"type":21,"value":175},"Convert HTML to PDF in Java with OpenHTMLtoPDF, and fix the header row",{"type":16,"tag":17,"props":177,"children":178},{},[179,181,188,190,196,198,204],{"type":21,"value":180},"Check the package name first. The top package pages for \"openhtmltopdf\" still point at ",{"type":16,"tag":182,"props":183,"children":185},"code",{"className":184},[],[186],{"type":21,"value":187},"com.openhtmltopdf",{"type":21,"value":189},", which stopped at 1.0.10 in September 2021. The project moved to ",{"type":16,"tag":182,"props":191,"children":193},{"className":192},[],[194],{"type":21,"value":195},"io.github.openhtmltopdf:openhtmltopdf-pdfbox",{"type":21,"value":197},", now at 1.2.2. Its README recommends parsing your HTML with jsoup first, so add ",{"type":16,"tag":182,"props":199,"children":201},{"className":200},[],[202],{"type":21,"value":203},"org.jsoup:jsoup",{"type":21,"value":205}," too:",{"type":16,"tag":207,"props":208,"children":213},"pre",{"code":209,"language":210,"meta":7,"className":211},"// OpenHTMLtoPDF 1.2.2 (io.github.openhtmltopdf:openhtmltopdf-pdfbox) and jsoup 1.24.1\n// https://github.com/openhtmltopdf/openhtmltopdf\nimport com.openhtmltopdf.pdfboxout.PdfRendererBuilder;\nimport org.jsoup.Jsoup;\nimport org.jsoup.helper.W3CDom;\nimport java.io.*;\nimport java.nio.file.*;\n\nString html = Files.readString(Path.of(\"invoice.html\"));\norg.w3c.dom.Document doc = new W3CDom().fromJsoup(Jsoup.parse(html));\ntry (OutputStream os = new FileOutputStream(\"invoice.pdf\")) {\n    PdfRendererBuilder builder = new PdfRendererBuilder();\n    builder.withW3cDocument(doc, new File(\".\").toURI().toString());\n    builder.toStream(os);\n    builder.run();\n}\n","java",[212],"language-java",[214],{"type":16,"tag":182,"props":215,"children":216},{"__ignoreMap":7},[217],{"type":21,"value":209},{"type":16,"tag":17,"props":219,"children":220},{},[221,223,229],{"type":21,"value":222},"It made 3 pages in 0.46 seconds in a fresh JVM, 0.03 to 0.04 seconds after that, and got two things wrong. The totals came out as \"Subtotal$6,600.00\", label and amount touching, because the rows are flexbox. And the table header printed on page 1 only, though our CSS sets ",{"type":16,"tag":182,"props":224,"children":226},{"className":225},[],[227],{"type":21,"value":228},"thead { display: table-header-group }",{"type":21,"value":230},". The script didn't run. That matches the README: no flexbox, no grid, no JavaScript.",{"type":16,"tag":232,"props":233,"children":235},"guide-figure",{"caption":234},"The invoice's second page, from PDFShift and from OpenHTMLtoPDF 1.2.2 with no fixes. Our test, December 3, 2026.",[236],{"type":16,"tag":237,"props":238,"children":240},"guide-page-pair",{":names":239},"[\"PDFShift API\", \"OpenHTMLtoPDF\"]",[241,252],{"type":16,"tag":17,"props":242,"children":243},{},[244],{"type":16,"tag":245,"props":246,"children":251},"img",{"alt":247,"height":248,"src":249,"width":250},"Page 2 of the invoice from PDFShift: the dark header row with Description, Qty, Unit price and Amount sits at the top, above work packages 019 to 040.",905,"/images/guides/html-to-pdf-java-pdfshift-page-2.webp",640,[],{"type":16,"tag":17,"props":253,"children":254},{},[255],{"type":16,"tag":245,"props":256,"children":259},{"alt":257,"height":248,"src":258,"width":250},"Page 2 of the invoice from OpenHTMLtoPDF: work packages 017 to 038 start at the top of the page with no header row above them.","/images/guides/html-to-pdf-java-openhtmltopdf-page-2.webp",[],{"type":16,"tag":17,"props":261,"children":262},{},[263],{"type":21,"value":264},"Both have a fix. Turn each totals row into a two-cell table, and switch on the engine's own table paging property:",{"type":16,"tag":207,"props":266,"children":268},{"code":267},".totals div { display: table; width: 100%; }\n.totals span { display: table-cell; }\n.totals span + span { text-align: right; }\ntable { -fs-table-paginate: paginate; }\n",[269],{"type":16,"tag":182,"props":270,"children":271},{"__ignoreMap":7},[272],{"type":21,"value":267},{"type":16,"tag":17,"props":274,"children":275},{},[276],{"type":21,"value":277},"With those four rules, and nothing changed in the HTML, OpenHTMLtoPDF printed label and amount apart and repeated the header on all 3 pages. That works for one template you control. For your app's own pages, every flex or grid layout needs a rewrite like this, and the PDF version drifts from your real stylesheet.",{"type":16,"tag":17,"props":279,"children":280},{},[281,283,289,291,297],{"type":21,"value":282},"Flying Saucer, the older engine OpenHTMLtoPDF grew from, failed sooner. Given our HTML5 file with ",{"type":16,"tag":182,"props":284,"children":286},{"className":285},[],[287],{"type":21,"value":288},"setDocumentFromString",{"type":21,"value":290},", it threw ",{"type":16,"tag":182,"props":292,"children":294},{"className":293},[],[295],{"type":21,"value":296},"The element type \"meta\" must be terminated by the matching end-tag \"\u003C/meta>\"",{"type":21,"value":298},". Passed through jsoup, it printed exactly what OpenHTMLtoPDF printed, and the same four rules fixed it.",{"type":16,"tag":24,"props":300,"children":302},{"id":301},"itext-pdfhtml-flexbox-without-a-browser-under-agpl",[303],{"type":21,"value":304},"iText pdfHTML: flexbox without a browser, under AGPL",{"type":16,"tag":17,"props":306,"children":307},{},[308],{"type":21,"value":309},"iText's HTML module is the one library here that got the layout right with no browser and no changes:",{"type":16,"tag":207,"props":311,"children":314},{"code":312,"language":210,"meta":7,"className":313},"// iText pdfHTML 6.3.4 (com.itextpdf:html2pdf), https://itextpdf.com/products/convert-html-css-to-pdf-pdfhtml\nimport com.itextpdf.html2pdf.HtmlConverter;\nimport java.io.*;\nimport java.nio.file.*;\n\ntry (OutputStream os = new FileOutputStream(\"invoice.pdf\")) {\n    HtmlConverter.convertToPdf(Files.readString(Path.of(\"invoice.html\")), os);\n}\n",[212],[315],{"type":16,"tag":182,"props":316,"children":317},{"__ignoreMap":7},[318],{"type":21,"value":312},{"type":16,"tag":17,"props":320,"children":321},{},[322],{"type":21,"value":323},"The totals came out with label and amount apart and the header row repeated on all 3 pages, in 0.06 to 0.08 seconds once the JVM was warm. It didn't run the script, so anything a script draws, like a chart, stays blank. Where it costs you is the licence. iText offers pdfHTML under the AGPL or a commercial licence, so check with whoever signs off on licences before it goes into a closed-source product.",{"type":16,"tag":24,"props":325,"children":327},{"id":326},"java-html-to-pdf-with-headless-chrome-playwrights-220-mb-driver",[328],{"type":21,"value":329},"Java HTML to PDF with headless Chrome: Playwright's 220 MB driver",{"type":16,"tag":17,"props":331,"children":332},{},[333],{"type":21,"value":334},"Playwright for Java drives a real Chromium, so the PDF matches what Chromium shows:",{"type":16,"tag":207,"props":336,"children":339},{"code":337,"language":210,"meta":7,"className":338},"// Playwright for Java 1.65.2 (com.microsoft.playwright:playwright), https://playwright.dev/java/\nimport com.microsoft.playwright.*;\nimport java.nio.file.*;\n\ntry (Playwright playwright = Playwright.create();\n     Browser browser = playwright.chromium().launch()) {\n    Page page = browser.newPage();\n    page.setContent(Files.readString(Path.of(\"invoice.html\")));\n    page.pdf(new Page.PdfOptions().setFormat(\"A4\").setPath(Path.of(\"invoice.pdf\")));\n}\n",[212],[340],{"type":16,"tag":182,"props":341,"children":342},{"__ignoreMap":7},[343],{"type":21,"value":337},{"type":16,"tag":17,"props":345,"children":346},{},[347],{"type":21,"value":348},"Everything came out right, and the script ran. After the first call, each PDF took 0.33 to 0.35 seconds.",{"type":16,"tag":17,"props":350,"children":351},{},[352,354,360,362,368],{"type":21,"value":353},"The cost lands in your build and on your servers. The Playwright jars we pulled weighed 224 MB, 220 MB of it one driver bundle. Our first ",{"type":16,"tag":182,"props":355,"children":357},{"className":356},[],[358],{"type":21,"value":359},"Playwright.create()",{"type":21,"value":361}," then spent 21 seconds downloading browsers: Chromium and its headless shell, 583 MB, plus Firefox and WebKit, 1.2 GB in all. PDFs only need Chromium, and Playwright's ",{"type":16,"tag":182,"props":363,"children":365},{"className":364},[],[366],{"type":21,"value":367},"install",{"type":21,"value":369}," command can fetch just that one. Either way, every container image or CI cache carries a browser, and you update it with each Playwright release.",{"type":16,"tag":24,"props":371,"children":373},{"id":372},"html-to-pdf-api-in-java-one-httpclient-call-no-browser",[374],{"type":21,"value":375},"HTML to PDF API in Java: one HttpClient call, no browser",{"type":16,"tag":17,"props":377,"children":378},{},[379,381,387],{"type":21,"value":380},"PDFShift renders your HTML in Chromium on our servers in France, and we've run PDFShift since 2018. Java has shipped ",{"type":16,"tag":182,"props":382,"children":384},{"className":383},[],[385],{"type":21,"value":386},"java.net.http.HttpClient",{"type":21,"value":388}," since Java 11, so the call needs only a JSON library, here Jackson:",{"type":16,"tag":207,"props":390,"children":393},{"code":391,"language":210,"meta":7,"className":392},"// PDFShift API v3, https://docs.pdfshift.io/, java.net.http.HttpClient (Java 11+) and Jackson\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport java.net.URI;\nimport java.net.http.*;\nimport java.nio.file.*;\nimport java.util.Map;\n\nString body = new ObjectMapper().writeValueAsString(Map.of(\n    \"source\", Files.readString(Path.of(\"invoice.html\")),\n    \"format\", \"A4\",\n    \"use_print\", true));\nHttpRequest request = HttpRequest.newBuilder(URI.create(\"https://api.pdfshift.io/v3/convert/pdf\"))\n    .header(\"X-API-Key\", System.getenv(\"PDFSHIFT_API_KEY\"))\n    .header(\"Content-Type\", \"application/json\")\n    .POST(HttpRequest.BodyPublishers.ofString(body))\n    .build();\nHttpResponse\u003Cbyte[]> response = HttpClient.newHttpClient()\n    .send(request, HttpResponse.BodyHandlers.ofByteArray());\nif (response.statusCode() != 200) {\n    throw new RuntimeException(\"PDFShift error: \" + new String(response.body()));\n}\nFiles.write(Path.of(\"invoice.pdf\"), response.body());\n",[212],[394],{"type":16,"tag":182,"props":395,"children":396},{"__ignoreMap":7},[397],{"type":21,"value":391},{"type":16,"tag":17,"props":399,"children":400},{},[401],{"type":21,"value":402},"We got the same PDF as Playwright: 3 pages, totals spaced right, the header row repeated, the script ran, 35 KB. The only thing we added to our machine was Jackson's 2.3 MB.",{"type":16,"tag":17,"props":404,"children":405},{},[406,408,414],{"type":21,"value":407},"Each request took 2.5 to 3.0 seconds, network round trip from our machine included. For a big batch of your app's pages, add ",{"type":16,"tag":182,"props":409,"children":411},{"className":410},[],[412],{"type":21,"value":413},"webhook",{"type":21,"value":415},": PDFShift sends a POST to your server when each PDF is done, and your threads don't sit waiting.",{"type":16,"tag":17,"props":417,"children":418},{},[419,421,427],{"type":21,"value":420},"To convert a web page instead of a string, put its URL in ",{"type":16,"tag":182,"props":422,"children":424},{"className":423},[],[425],{"type":21,"value":426},"source",{"type":21,"value":428},". In Spring Boot, the same client works from any service: render your Thymeleaf template to a string, then send that string. Each PDF up to 5 MB costs 1 credit.",{"type":16,"tag":17,"props":430,"children":431},{},[432,439,441,447,449,455,457,463],{"type":16,"tag":433,"props":434,"children":436},"a",{"href":435},"/register/",[437],{"type":21,"value":438},"Create a free account",{"type":21,"value":440}," for 50 credits a month, no card needed. The same call with OkHttp, Retrofit or Netty, plus headers, cookies and watermarks, is in ",{"type":16,"tag":433,"props":442,"children":444},{"href":443},"/guides/java/",[445],{"type":21,"value":446},"our Java guides",{"type":21,"value":448},". We ran the same kind of test for ",{"type":16,"tag":433,"props":450,"children":452},{"href":451},"/guides/html-to-pdf-php/",[453],{"type":21,"value":454},"PHP",{"type":21,"value":456}," and ",{"type":16,"tag":433,"props":458,"children":460},{"href":459},"/guides/html-to-pdf-python/",[461],{"type":21,"value":462},"Python",{"type":21,"value":464},".",{"title":7,"searchDepth":466,"depth":466,"links":467},2,[468,469,470,471,472],{"id":26,"depth":466,"text":29},{"id":172,"depth":466,"text":175},{"id":301,"depth":466,"text":304},{"id":326,"depth":466,"text":329},{"id":372,"depth":466,"text":375},"markdown","content:guides:html-to-pdf-java.md","content","guides/html-to-pdf-java.md","md",1796486664893]