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

Problem when load a font #425

Closed
fabioebner opened this issue Dec 11, 2019 · 5 comments
Closed

Problem when load a font #425

fabioebner opened this issue Dec 11, 2019 · 5 comments

Comments

@fabioebner
Copy link

I create one project that convert a HTML to PDF/A then I deploy that project in a repository. :

`
public class PdfService {

public File FONT;
public File COLOR;

public PdfService() {
    setFont(new File(getClass().getClassLoader().getResource("NotoSerif-Regular.ttf").getFile()),
            new File(getClass().getClassLoader().getResource("sRGB.icc").getFile()));
}

public PdfService(File font, File color) {
    setFont(font, color);
}

private void setFont(File font, File color) {
    this.FONT = font;
    this.COLOR = color;
}



public File htmlToPdf1A(String html) throws Exception {
    File arquivo = File.createTempFile(UUID.randomUUID().toString(),".PDF");

    return htmlToPdf1A(html, arquivo);
}

public File htmlToPdf1A(String html, File arqDestino) throws Exception {
    System.out.println(FONT.getPath());
    PdfRendererBuilder.PdfAConformance conform = PdfRendererBuilder.PdfAConformance.PDFA_1_A;
    PdfRendererBuilder builder = new PdfRendererBuilder();
    PDDocument doc = new PDDocument();
    builder.usePDDocument(doc);
    builder.useFastMode();
    builder.useFont(this.FONT, "noto");
    builder.withUri("file://"+FONT.getPath());
    builder.usePdfVersion(conform.getPart() == 1 ? 1.4f : 1.5f);
    builder.usePdfAConformance(conform);

    String htmlCorrigido = tidyHtml(html, true);


    htmlCorrigido = htmlCorrigido.replace("<head>","<head> <style>" +
            " blockquote {\n" +
            "     border-left: .25em solid #dfe2e5;\n" +
            "     color: #6a737d;\n" +
            "     padding-left: 1em;\n" +
            "     margin: 20px 0!important;\n" +
            " }\n" +
            "@font-face {\n" +
            "  font-family: 'noto';\n" +
            "  /* Note: No trailing format tag is allowed. */\n" +
            "  src: url(file://"+FONT.getAbsolutePath()+");\n" +
            "  font-weight: normal;\n" +
            "  font-style: normal;\t\n" +
            "}\n" +
            "" +
            ".noto {\n" +
            "  font-family: 'noto', serif;\n" +
            "}\n" +
            "</style>");
    htmlCorrigido = htmlCorrigido.replace("<body>","<body class='noto editor'>");

    builder.withHtmlContent(htmlCorrigido, null);
    OutputStream baos = new FileOutputStream(arqDestino);
    builder.toStream(baos);
    builder.run();
    System.out.println(arqDestino.getAbsolutePath());
    return arqDestino;
}

private String tidyHtml(String htmlString, boolean isHtmlCompelto) {
    Tidy tidy = new Tidy();
    tidy.setDocType("omit");
    tidy.setXmlOut(true);
    tidy.setQuiet(true);
    tidy.setTidyMark(false);
    //Entender como funciona setWrapLen - Heryk
    tidy.setWraplen(100000);
    StringWriter swError = new StringWriter();
    tidy.setErrout(new PrintWriter(swError));
    StringWriter sw = new StringWriter();
    tidy.parse(new StringReader(htmlString), sw);
    String tidyHtml = sw.getBuffer().toString();
    tidyHtml = tidyHtml.replaceAll("(\r\n|\n|\r)+\\s*<", "<");
    if(!isHtmlCompelto){
        tidyHtml = tidyHtml.replace("<html>","");
        tidyHtml = tidyHtml.replace("<head>","");
        tidyHtml = tidyHtml.replace("<title>","");
        tidyHtml = tidyHtml.replace("</title>","");
        tidyHtml = tidyHtml.replace("</head>","");
        tidyHtml = tidyHtml.replace("<body>","");
        tidyHtml = tidyHtml.replace("</body>","");
        tidyHtml = tidyHtml.replace("</html>","");

    }
    return tidyHtml;
}

}`

Then I use that component in a springboot application so when I call my comp with this code:

PdfService pdfService = new PdfService(new File("/Users/fabioebner/Projetos/java/springboot/notificato-api/src/main/resources/NotoSerif-Regular.ttf"),new File("/Users/fabioebner/Projetos/java/springboot/notificato-api/src/main/resources/sRGB.icc"));

works fine. but when I use the ResourceLoader to load the font and color (with this code)
PdfService pdfService = new PdfService(resourceLoader.getResource("classpath:NotoSerif-Regular.ttf").getFile(),resourceLoader.getResource("classpath:sRGB.icc").getFile());

he can't find read the font..

anyone knwo why?

tks

the message:

com.openhtmltopdf.load INFO:: Loading font(noto) from InputStream supplier now.
com.openhtmltopdf.exception WARNING:: Couldn't load font. Please check that it is a valid truetype font.
com.openhtmltopdf.exception WARNING:: Font metrics not available. Probably a bug.
com.openhtmltopdf.exception WARNING:: Font metrics not available. Probably a bug.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.general INFO:: Using fast-mode renderer. Prepare to fly.

@syjer
Copy link
Contributor

syjer commented Dec 11, 2019

hi @fabioebner ,

are you sure that resourceLoader.getResource("classpath:NotoSerif-Regular.ttf").getFile() return a file that can be then opened?

Quite sure this is the issue :).

@fabioebner
Copy link
Author

hi @syjer

my log:

2019-12-11 19:50:22.794 DEBUG 29122 --- [nio-8282-exec-2] b.c.l.n.api.controller.PedidoController : arquivo /Users/fabioebner/Projetos/java/springboot/notificato-api/target/classes/NotoSerif-Regular.ttf
2019-12-11 19:50:22.794 DEBUG 29122 --- [nio-8282-exec-2] b.c.l.n.api.controller.PedidoController : canRead true
2019-12-11 19:50:22.794 DEBUG 29122 --- [nio-8282-exec-2] b.c.l.n.api.controller.PedidoController : canExecute false

@syjer
Copy link
Contributor

syjer commented Dec 11, 2019

@fabioebner are you building your project with maven?

If you have put your fonts in the src/main/resources folder you may have the same issue as #360 (comment) (where the binary files are filtered (and modified) by the maven-resources-plugin )

@danfickle
Copy link
Owner

Generally, resources will be files while building with an IDE but not in the final jar. Better to use input streams.

Also:

  • use either builder.usefont or a font-face rule, not both.
  • use either withuri or withhtmlcontent not both.
  • the second argument of withHtmlContent should be the base uri.

@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

3 participants