LibrePDF / OpenPDF

OpenPDF is a free Java library for creating and editing PDF files, with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generating PDF from HTML containing CSS font-size property fails with IllegalArgumentException

RadekWikturna opened this issue · comments

Describe the bug
When converting HTML to PDF, using HTMLWorker.parseToList and the HTML contains the CCS font-size property, such as in the test below, rendering to PDF fails with

IllegalArgumentException: font size too small: 0.0

To Reproduce

@Test	
public void testCCSFonts() throws Exception {
	StringReader reader = new StringReader("<span style=\"font-size:xx-small\">Text xx-small</span><br/> <span style=\"font-size:x-small\">Text x-small</span><br/> <span style=\"font-size:small\">Text small</span><br/> <span style=\"font-size:medium\">Text medium</span><br/> <span style=\"font-size:large\">Text large</span><br/> <span style=\"font-size:x-large\">Text x-large</span><br/> <span style=\"font-size:xx-large\">Text xx-large</span><br/>");
	StyleSheet styleSheet = new StyleSheet();
	Map<String, Object> interfaceProps = new HashMap<>();
	List<Element> elements = HTMLWorker.parseToList(reader, styleSheet, interfaceProps);
		
        Document document = new Document();
        PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream("CCS Fonts.pdf"));
        document.open();
        instance.getInfo().put(PdfName.CREATOR, new PdfString(Document.getVersion()));
        for(Element e: elements) {
        	document.add(e);
        }
        document.close();
}

Expected behavior
The generated PDF should show approximately the same output as the HTML in a browser and should not fail with exception.

Screenshots
The HTML snippet below shows correctly in a browser:
image

System (please complete the following information):

  • OS: Windows 11
  • Used Font: default

Pull requests are welcome!