Last active
November 21, 2018 14:01
-
-
Save trajano/8b1f4b5f961e867e6c78b581dba0ca72 to your computer and use it in GitHub Desktop.
PDFBOX-3903
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.openhtmltopdf.DOMBuilder; | |
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder; | |
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.springframework.stereotype.Component; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
@Component | |
public class HtmlToPdf { | |
public void convert(String html, OutputStream os) { | |
Document document = Jsoup.parse(html); | |
this.convert(document, os); | |
} | |
public void convert(Document document, OutputStream os) { | |
document.head().append("<style>@page { size: 975px 1248px; margin: 10px; } body { font-family: 'noto', sans-serif;} td { text-align: left; } td[align='right'] { text-align: right; }</style>"); | |
try { | |
new PdfRendererBuilder() | |
.withW3cDocument(DOMBuilder.jsoup2DOM(document), "/") | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSansCJKtc-Regular.ttf"), "noto") | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSansCJKtc-Bold.ttf"), "noto", 700, BaseRendererBuilder.FontStyle.NORMAL, true) | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoColorEmoji.ttf"), "noto") | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoEmoji-Regular.ttf"), "noto") | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSansKhmer-Regular.ttf"), "noto") | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSans-Regular.ttf"), "noto") | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSans-Bold.ttf"), "noto", 700, BaseRendererBuilder.FontStyle.NORMAL, true) | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSans-Italic.ttf"), "noto", 400, BaseRendererBuilder.FontStyle.ITALIC, true) | |
.useFont(() -> Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/NotoSans-BoldItalic.ttf"), "noto", 700, BaseRendererBuilder.FontStyle.ITALIC, true) | |
.toStream(os) | |
.run(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public void convert(InputStream is, OutputStream os) throws IOException { | |
Document document = Jsoup.parse(is, "utf8", "/test.html"); | |
this.convert(document, os); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testCreatePdfWithComplexCharacters() throws Exception { | |
HtmlToPdf htmlToPdf = new HtmlToPdf(); | |
File t = File.createTempFile("test", ".pdf"); | |
try (OutputStream os = new FileOutputStream(t)) { | |
htmlToPdf.convert(this.getClass().getResourceAsStream("/test.html"), os); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} finally { | |
t.delete(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>hello</title> | |
</head> | |
<body> | |
<p>Objective: トレンドがわかる、気になるファッションランキング</p> | |
<p>Simplified 你好世界在学校</p> | |
<p>Traditional 你好世界在學校</p> | |
<p>Cambodian ភាសាខ្មែរ</p> | |
🤷 Person Shrugging | |
♡ White Heart Suit | |
❤ Red Heart | |
🎃 Jack-O-Lantern | |
😂 Face With Tears of Joy | |
😍 Smiling Face With Heart-Eyes | |
🤔 Thinking Face | |
😊 Smiling Face With Smiling Eyes | |
🔥 Fire | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment