Skip to content
New issue

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

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

Already on GitHub? # to your account

using withHtmlContent method result #### in Arabic #53

Closed
yalmuhaidib opened this issue Nov 25, 2016 · 6 comments
Closed

using withHtmlContent method result #### in Arabic #53

yalmuhaidib opened this issue Nov 25, 2016 · 6 comments

Comments

@yalmuhaidib
Copy link

I'm using thymeleaf to process the HTML page that would be then passed to openhtmltopdf to generate a PDF with Arabic content.
the issue if I'm trying to generate using
withHtmlContent(String html, String baseUri)
then:

  • the PDF will have #### for any Arabic Content.
  • what is baseUri and how I should set it?
  • if I just pass the HTML page location using withUri(String uri), Arabic is rendered, the issue with this is I need to process the HTML to be dynamic "generate a purchase order"

check below sample code:

public void generateOpenPDF() throws Exception {
    String htmlContent = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><style>@font-face{font-family: noto; src: url('NotoNaskhArabic-Regular.ttf');}body, body *{font-family: 'noto', sans-serif;}</style></head><body><div class=\"border\"><h1>عربي</h1></div></body></html>";

   ByteArrayOutputStream os = new ByteArrayOutputStream();
   PdfRendererBuilder builder = new PdfRendererBuilder();
   builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
   builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
   builder.defaultTextDirection(TextDirection.RTL); // OR RTL
	    
   builder.withHtmlContent(htmlContent, "/tmp");
        
   builder.toStream(os);
   builder.run();
   byte[] pdfAsBytes = os.toByteArray();
   os.close();
   Path file = Paths.get("/tmp/the-file-name.pdf");
   Files.write(file, pdfAsBytes);
}
@danfickle
Copy link
Owner

Hi @yalmuhaidib
Sorry for the late reply. You use a relative link to the font in the HTML, so you need a base URL to resolve relative URLs. Try using a file:// URL.

builder.withHtmlContent(htmlContent, "file:///absolute/path/to/font/dummy.html");

@JimTheod
Copy link

hi!

Same problem with greeks i can not understand the content of the dummy.html
Any example would be usefull.
When i use the builder.withUri(url); and in the html there is the reference to the font works fine.
I can not do to work with builder.withHtmlContent(htmlContent...

thanx

@JimTheod
Copy link

Solved!
One solution is to add

<style>
@font-face {
    font-family: 'cjk';
    src: url('file:///d:/deleme/java/webServices/WebServicesTools/resources/Calibri.ttf');
}

body
{
font-family:cjk;
}
</style>

inside htmldata and leave the second arg of withHtmlContent ""

@danfickle
Copy link
Owner

Hi @JimTheod
Yes, using complete paths is one solution. However, you should be able to use relative paths. The dummy.html doesn't actually have to exist. It's the path to your relative resources that is important. Sorry for the confusion. So in your case:

<style>
@font-face {
    font-family: 'cjk';
    src: url('Calibri.ttf');
}

body
{
font-family:cjk;
}
</style>

and

builder.withHtmlContent(htmlContent, "file:///file:///d:/deleme/java/webServices/WebServicesTools/resources/dummy.htm");

I've tested this, but on a mac, maybe it is not working in Windows?

@JimTheod
Copy link

Thanx for quiq reply!

Yes you are right if i use

<style>
@font-face {
    font-family: 'cjk';
    src: url('Calibri.ttf');
}

body
{
font-family:cjk;
}
</style>

and then set only the path works fine

builder.withHtmlContent(htmlContent, "file:///file:///d:/deleme/java/webServices/WebServicesTools/resources/");

@ieugen
Copy link
Contributor

ieugen commented Feb 12, 2020

@danfickle , @JimTheod : This seems to be solved. No reason to keep the issue open.

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

No branches or pull requests

4 participants