Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When converting HTML to PDF, background color is not applied #1061

Closed
RadekWikturna opened this issue Feb 20, 2024 · 5 comments · Fixed by #1075
Closed

When converting HTML to PDF, background color is not applied #1061

RadekWikturna opened this issue Feb 20, 2024 · 5 comments · Fixed by #1075
Assignees
Milestone

Comments

@RadekWikturna
Copy link
Contributor

Describe the bug
When converting HTML to PDF, using HTMLWorker.parseToList, background color is not applied. I tried 2 ways of setting the background color - one was produced by CK Editor, the other one I'm not sure, perhaps Word. Maybe there are other ways, I'm not an HTML expert.

To Reproduce

	@Test	
	public void testBackgroundColor() throws Exception {
		StringReader reader = new StringReader(
				"<span style=\"background-color:#f39c12\">Orange background</span><br/>"
				+ "<span style=\"background: none repeat scroll 0% 0% firebrick;\">Firebrick background</span>");
		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("Background color.pdf"));
		document.open();
		instance.getInfo().put(PdfName.CREATOR, new PdfString(Document.getVersion()));
		for(Element e: elements) {
			document.add(e);
		}
		document.close();
	}

Expected behavior
Background color should be properly interpreted and shown approximately the same as the HTML in a browser.

Screenshots
HTML in browser:
image
Generated PDF:
image

System:

  • OS: Windows 11
  • Used Font: default
@asturio
Copy link
Member

asturio commented Feb 20, 2024

Please take a look at flyingsaucer: https://github.com/flyingsaucerproject/flyingsaucer

@asturio
Copy link
Member

asturio commented Feb 20, 2024

Pull request are welcome.

@RadekWikturna
Copy link
Contributor Author

RadekWikturna commented Feb 20, 2024

Regarding https://github.com/flyingsaucerproject/flyingsaucer, this is something I've not heard of before. It appears to take XML or XHTML + CSS on input and produce PDF output. However, in my project I need to generate PDF programmatically combining various elements (plain text, tables, images) using database as a the data source, and the HTML needs to be rendered in some particular area on a page (e.g. in a column or in a paragraph, or in a table cell). So I don't think I can use flyingsaucer to do that (to accept a string containing HTML and return PDF elements that I could add to a PDF document).
It also clearly says : "No support for legacy or "street" HTML"
The worst thing is that it uses iText, which in our project we are trying to replace with OpenPDF.

@RadekWikturna
Copy link
Contributor Author

I'll try to fix it

RadekWikturna pushed a commit to RadekWikturna/OpenPDF that referenced this issue Feb 23, 2024
asturio pushed a commit that referenced this issue Feb 23, 2024
…1075)

* #1061 When converting HTML to PDF, background color is not applied

---------

Co-authored-by: Radek Wikturna <radek.wikturna@usu.com>
@asturio asturio self-assigned this Feb 23, 2024
@asturio asturio modified the milestones: 1.3.42, 1.4.2, 2.0.1 Feb 23, 2024
@asturio asturio linked a pull request Feb 23, 2024 that will close this issue
1 task
asturio pushed a commit that referenced this issue Feb 23, 2024
…1075)

* #1061 When converting HTML to PDF, background color is not applied

---------

Co-authored-by: Radek Wikturna <radek.wikturna@usu.com>
asturio pushed a commit that referenced this issue Feb 23, 2024
…1075)

* #1061 When converting HTML to PDF, background color is not applied

---------

Co-authored-by: Radek Wikturna <radek.wikturna@usu.com>
@asturio
Copy link
Member

asturio commented Feb 26, 2024

Release is coming

@asturio asturio closed this as completed Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants