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

In line font color not printing specified color code, #736

Closed
mgabhishek06kodur opened this issue Jul 16, 2021 · 6 comments
Closed

In line font color not printing specified color code, #736

mgabhishek06kodur opened this issue Jul 16, 2021 · 6 comments

Comments

@mgabhishek06kodur
Copy link

I'm tried to print font with color it is not printing specified font color, in inline style

import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.w3c.dom.Document;
import com.lmats.oms.reports.pdfreports.PdfReportUtils;
import com.lmats.oms.reports.pdfreports.ReportPage;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;

private static String htmlToPdf(String inputHTML, String outputPdf) throws IOException {
	Document doc = html5ParseDocument(inputHTML);
	String baseUri = FileSystems.getDefault().getPath(AppConstants.GENERAL_SECTION5TEMP).toUri().toString();
	OutputStream os = new FileOutputStream(outputPdf);
	PdfRendererBuilder builder = new PdfRendererBuilder();
	builder.withUri(outputPdf);
	builder.toStream(os);
	builder.useFont(new File(AppConstants.FOLDER_FONTS   "Calibri.ttf"), "Calibri");
	builder.withW3cDocument(doc, baseUri);
	builder.run();
	logger.debug("PDF generation completed in html to pdf");
	os.close();
	return outputPdf;
}

private static Document html5ParseDocument(String inputHTML) throws IOException {
	org.jsoup.nodes.Document doc;
	logger.debug("parsing ...");
	doc = Jsoup.parse(new File(inputHTML), "UTF-8");
	logger.debug("parsing done ..."   doc);
	return new W3CDom().fromJsoup(doc);
}
@mgabhishek06kodur mgabhishek06kodur changed the title In line font color not printing specified color, In line font color not printing specified color code, Jul 16, 2021
@danfickle
Copy link
Owner

Could you post the HTML in question? It should look something like:

<span style="color: #f1f100;">some text</span>

@mgabhishek06kodur

This comment has been minimized.

@mgabhishek06kodur

This comment has been minimized.

@mgabhishek06kodur
Copy link
Author

samplehtmltext.txt

@danfickle
Copy link
Owner

danfickle commented Jul 17, 2021

Hi @mgabhishek06kodur,

We don't support the attributes on the ancient font tag. However, if you can't use CSS instead in your template, you can use a DOM mutator to change the document at runtime. Here is one I just created:

        FSDOMMutator domChanger = (doc) -> {
            NodeList fontTags = doc.getElementsByTagName("font");

            for (int i = 0; i < fontTags.getLength(); i  ) {
                Element fontTag = (Element) fontTags.item(i);

                if (fontTag.hasAttribute("color")) {
                    String color = fontTag.getAttribute("color");

                    if (!fontTag.hasAttribute("style")) {
                        fontTag.setAttribute("style", "color: "   color   ';');
                    } else {
                        String oldStyle = fontTag.getAttribute("style");
                        String newStyle = oldStyle   "; color: "   color   ';';

                        fontTag.setAttribute("style", newStyle);
                    }
                }
            }
        };

Then, you can register with:

builder.addDOMMutator(domChanger);

P.S. To paste code or HTML in comments, put it in a code block. A code block starts with four backticks on their own line, followed by the HTML, then four more backticks on their own line. This is a backtick: `

Also added FAQ entry on dom mutators

@danfickle
Copy link
Owner

Assumed solved. Please reopen as required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants