I think you can open you html file in Chrome. Then print it to pdf format. Then it will works.

That is when you print it, you choose "save as pdf" rather than your printer.

Answer from daizuozhuo on Stack Overflow
🌐
Adobe
adobe.com › acrobat › resources › how-to-convert-html-to-pdf.html
How to convert HTML to PDF | Adobe Acrobat
PDF (Portable Document Format) files are perfect for maintaining text formatting and the sharpness of charts, graphs, and high-quality photos. They meet ISO image standards so you know your colors will stay true. Others who open your files using a PDF viewer such as Acrobat Reader will see your content exactly as you intended. When you export HTML to PDF, you don’t lose the HTML file or associated files like JPEG or PNG images, cascading style sheets, text files, image maps, or forms.
🌐
Pdfgear
pdfgear.com › home › pdf converter › how to convert html to pdf on windows and mac without losing formatting
How to Convert HTML to PDF on Windows and Mac in 5 Effective Ways
September 25, 2025 - If the input format is URL, then go to the “Convert URL to PDF” tab > Type in the URL of the web page > Click “ADD”. ... Step 3. After completing the conversion, click “Download” to download the PDF file converted from HTML. ... Enable the Microsoft Print to PDF feature on your Windows computer, and you can easily print files to PDF on Windows 10 and 11 without additional software download.
🌐
Sejda PDF
sejda.com › html-to-pdf
Convert HTML to PDF Online
3 You're done! Now load a web page in your browser and click the 'Save as PDF' bookmark to convert it to a PDF document. Multiple web pages can be converted at a time. Paste each URL on a separate line. Click Convert HTML to PDF and wait until processing completes.
🌐
Docupilot
docupilot.com › tools › html-to-pdf
Convert HTML to PDF: A Free Online Converter
Docupilot
Docupilot is a document automation platform that helps businesses streamline document creation. It allows users to design custom templates or import from Word, Excel, PowerPoint, and PDF, then automatically generate contracts, invoices, reports, and letters by merging templates with data from CRMs, spreadsheets, and databases. With automated workflows, integrations with Zapier, Salesforce, and Google Sheets, and collaboration tools for teams, Docupilot reduces manual effort, ensures accuracy, and improves productivity for industries including legal, finance, real estate, and marketing.
Price   $29.00
🌐
HTML2PDF
html2pdf.com
HTML to PDF – Convert HTML files to PDF
Our tool here can convert an HTML file into a PDF. You can upload a simple HTML file or a ZIP file containing the HTML code along with its corresponding images and style sheets. The first thing you need to do is decide how you’ll want your HTML rendered. There are four options on the page: Grayscale, Landscape, No Background, and No JavaScript. Ticking the Grayscale box will render your page without color; Landscape creates PDF pages in landscape mode rather than portrait; No Background will render the page with a simple white background; No JavaScript will remove all JavaScript from the page.
Find elsewhere
🌐
Smallpdf
smallpdf.com › pdf converter › how to convert pdf documents › html pdf conversion: formatting issues & how to resolve them
HTML PDF Conversion: Formatting Issues & How To Resolve Them | Smallpdf
March 14, 2025 - Convert HTML to PDF or PDF to HTML without losing formatting—fix broken layouts and missing styles. Here’s how to keep your documents perfect.
🌐
FreeConvert
freeconvert.com › html-to-pdf
HTML to PDF | Free Online HTML to PDF Converter
Simply upload your HTML files or enter webpage URLs and then hit the “Convert” button to start converting to PDF format.
🌐
PDFSimpli
pdfsimpli.com › home › pdf converters › html to pdf
Convert HTML to PDF Online - Easy & Quick
July 8, 2025 - Click "Convert" and select the desired file format. ... Download the converted file or share it with anyone. ... Free editing tools, plus other valuable features. ... Use PDFSimpli's online tool. Upload your HTML file or paste a URL, wait for the conversion, and download your converted PDF.
🌐
PDFCrowd
pdfcrowd.com › html-to-pdf
Convert HTML to PDF Online - PDFCrowd
For quicker conversion, utilize our Save as PDF Browser Addon or bookmarklet. These tools allows you to convert web pages with just a click. To install the bookmarklet, drag the button below to your bookmark bar (video tutorial). ... Turn any webpage or HTML into PDF programmatically.
🌐
Smallpdf
smallpdf.com › pdf converter › how to convert pdf documents › how to convert html with images to pdf without losing formatting
How to Convert HTML with Images to PDF Without Losing Formatting | Smallpdf
March 27, 2025 - ... Online converters are often the most reliable way to convert HTML with images to PDF, especially when pages include JavaScript, custom fonts, or complex layouts. 1. Go to an online HTML to PDF converter. 2. Upload your HTML file or paste ...
🌐
Systweak
systweak.com › home › how to covert html to pdf without losing formatting
HTML to PDF: The Easiest Way to Convert!
November 30, 2023 - Want to share an HTML document with a friend, but they may not have the appropriate tools to open it? Well, you are not alone, as thousands of users face a similar issue while sharing HTML files. The good news is that HTML files can be converted to PDF documents without changing the formatting or design of it.
Top answer
1 of 3
8

I used ITextRenderer from the Flying Saucer project.

Here is a short, self-contained, working example. In my case I wanted to later stream the bytes into an email attachment.

So, in the example I write it to a file purely for the sake of demonstration for this question. This is Java 8.

import com.lowagie.text.DocumentException;
import org.apache.commons.io.FileUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class So4712641 {

  public static void main(String... args) throws DocumentException, IOException {
    FileUtils.writeByteArrayToFile(new File("So4712641.pdf"), toPdf("<b>You gotta walk and don't look back</b>"));
  }

  /**
   * Generate a PDF document
   * @param html HTML as a string
   * @return bytes of PDF document
   */
  private static byte[] toPdf(String html) throws DocumentException, IOException {
    final ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    try (ByteArrayOutputStream fos = new ByteArrayOutputStream(html.length())) {
      renderer.createPDF(fos);
      return fos.toByteArray();
    }
  }
}

This gives me

For completeness, here are relevant pieces for my Maven pom.xml

<dependencies>
    <dependency>
        <groupId>org.xhtmlrenderer</groupId>
        <artifactId>flying-saucer-pdf</artifactId>
        <version>9.0.8</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>
2 of 3
0

Converting HTML to PDF isn't exactly straightforward in general, but if you're in control of what goes into the HTML, you can try using an XSL-FO implementation, like Apache FOP.

It's not out-of-the-box as you'll have to write (or find) a stylesheet that defines the conversion rules, but on the upside it gives you much more control over output formatting, which is quite useful as what looks good on screen doesn't necessarily look good on paper.

🌐
PDFgear
pdfgear.com › html-to-pdf
Convert HTML to PDF Online Free
Convert HTML to PDF on Windows, Mac, or online using Safari, Edge, Chrome, Adobe, or free converters for easy sharing. How to Convert TXT to PDF Without Losing Formatting
🌐
iLovePDF
ilovepdf.com › blog › convert-web-to-pdf
How to convert HTML to PDF
We show you how to save a webpage as a PDF with our free online converter. Export HTML data into PDF preserving formatting with accuracy.
🌐
Quora
quora.com › Is-it-possible-to-convert-an-HTML-file-to-a-PDF-without-damaging-its-appearance-that-means-without-damaging-its-content-its-text
Is it possible to convert an HTML file to a PDF without damaging its appearance? (that means without damaging its content, its text) - Quora
Answer: Maybe, if the HTML is well written then it is certainly won’t, a CSS tag is meant for printing or not printing, a skilled HTML programmer, or programmer that tasked to generate HTML output will know to use that tag to format it for printing. Even if it is not well written… still going to...
🌐
Adobe
adobe.com › acrobat › how-to › convert-html-to-pdf.html
Convert HTML pages to PDF
Learn how to easily convert web pages into PDF format with easy-to-share, easy-to-print convenience. No coding. No magic. No headaches. ... Use online tools in the Adobe PDF toolbar to save HTML files, entire web pages, or just part of a web page as PDFs right from your web browser.
🌐
iSkysoft
pdf.iskysoft.com › create-pdf › convert-html-to-pdf.html
4 Easist Way to Convert HTML to PDF
PDFelement is valuable in so many ways since it is not just an HTML to PDF converter, but a complete PDF management tool that is invaluable to businesses and individuals handling a lot of documents in their day-to-day lives. Some of the features that make it the ideal solution for PDF management include the following: ... It makes the creation of PDF files very easy. You simply need to open a document in any format using this program and a PDF file is instantly created.