-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0d3775e
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
target | ||
out | ||
.idea | ||
*.iml |
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
This Font Software is licensed under the SIL Open Font License, Version 1.1. | ||
This license is copied below, and is also available with a FAQ at: | ||
http://scripts.sil.org/OFL | ||
|
||
|
||
----------------------------------------------------------- | ||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 | ||
----------------------------------------------------------- | ||
|
||
PREAMBLE | ||
The goals of the Open Font License (OFL) are to stimulate worldwide | ||
development of collaborative font projects, to support the font creation | ||
efforts of academic and linguistic communities, and to provide a free and | ||
open framework in which fonts may be shared and improved in partnership | ||
with others. | ||
|
||
The OFL allows the licensed fonts to be used, studied, modified and | ||
redistributed freely as long as they are not sold by themselves. The | ||
fonts, including any derivative works, can be bundled, embedded, | ||
redistributed and/or sold with any software provided that any reserved | ||
names are not used by derivative works. The fonts and derivatives, | ||
however, cannot be released under any other type of license. The | ||
requirement for fonts to remain under this license does not apply | ||
to any document created using the fonts or their derivatives. | ||
|
||
DEFINITIONS | ||
"Font Software" refers to the set of files released by the Copyright | ||
Holder(s) under this license and clearly marked as such. This may | ||
include source files, build scripts and documentation. | ||
|
||
"Reserved Font Name" refers to any names specified as such after the | ||
copyright statement(s). | ||
|
||
"Original Version" refers to the collection of Font Software components as | ||
distributed by the Copyright Holder(s). | ||
|
||
"Modified Version" refers to any derivative made by adding to, deleting, | ||
or substituting -- in part or in whole -- any of the components of the | ||
Original Version, by changing formats or by porting the Font Software to a | ||
new environment. | ||
|
||
"Author" refers to any designer, engineer, programmer, technical | ||
writer or other person who contributed to the Font Software. | ||
|
||
PERMISSION & CONDITIONS | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of the Font Software, to use, study, copy, merge, embed, modify, | ||
redistribute, and sell modified and unmodified copies of the Font | ||
Software, subject to the following conditions: | ||
|
||
1) Neither the Font Software nor any of its individual components, | ||
in Original or Modified Versions, may be sold by itself. | ||
|
||
2) Original or Modified Versions of the Font Software may be bundled, | ||
redistributed and/or sold with any software, provided that each copy | ||
contains the above copyright notice and this license. These can be | ||
included either as stand-alone text files, human-readable headers or | ||
in the appropriate machine-readable metadata fields within text or | ||
binary files as long as those fields can be easily viewed by the user. | ||
|
||
3) No Modified Version of the Font Software may use the Reserved Font | ||
Name(s) unless explicit written permission is granted by the corresponding | ||
Copyright Holder. This restriction only applies to the primary font name as | ||
presented to the users. | ||
|
||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font | ||
Software shall not be used to promote, endorse or advertise any | ||
Modified Version, except to acknowledge the contribution(s) of the | ||
Copyright Holder(s) and the Author(s) or with their explicit written | ||
permission. | ||
|
||
5) The Font Software, modified or unmodified, in part or in whole, | ||
must be distributed entirely under this license, and must not be | ||
distributed under any other license. The requirement for fonts to | ||
remain under this license does not apply to any document created | ||
using the Font Software. | ||
|
||
TERMINATION | ||
This license becomes null and void if any of the above conditions are | ||
not met. | ||
|
||
DISCLAIMER | ||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT | ||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE | ||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL | ||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM | ||
OTHER DEALINGS IN THE FONT SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<packaging>jar</packaging> | ||
|
||
<name>JavaMarkdownToPdf</name> | ||
<groupId>soselab.stanley</groupId> | ||
<artifactId>JavaMarkdownToPdf</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core --> | ||
<dependency> | ||
<groupId>com.itextpdf</groupId> | ||
<artifactId>itext7-core</artifactId> | ||
<version>7.1.12</version> | ||
<type>pom</type> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf --> | ||
<dependency> | ||
<groupId>com.itextpdf</groupId> | ||
<artifactId>html2pdf</artifactId> | ||
<version>3.0.1</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.commonjava.googlecode.markdown4j/markdown4j --> | ||
<dependency> | ||
<groupId>org.commonjava.googlecode.markdown4j</groupId> | ||
<artifactId>markdown4j</artifactId> | ||
<version>2.2-cj-1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<release>11</release> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>soselab.stanley.JavaMarkdownToPdf.JavaMarkdownToPdf</mainClass> | ||
</manifest> | ||
</archive> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
104 changes: 104 additions & 0 deletions
104
src/main/java/soselab/stanley/JavaMarkdownToPdf/JavaMarkdownToPdf.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package soselab.stanley.JavaMarkdownToPdf; | ||
|
||
import com.itextpdf.html2pdf.ConverterProperties; | ||
import com.itextpdf.html2pdf.HtmlConverter; | ||
import com.itextpdf.layout.font.FontProvider; | ||
import org.markdown4j.Markdown4jProcessor; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
public class JavaMarkdownToPdf { | ||
private final Markdown4jProcessor markdown4jProcessor; | ||
private String title; | ||
|
||
public JavaMarkdownToPdf() { | ||
markdown4jProcessor = new Markdown4jProcessor(); | ||
} | ||
|
||
public byte[] convert(String markdown) { | ||
try { | ||
return getPdf(fixHtml(markdown4jProcessor.process(markdown).trim())); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
public JavaMarkdownToPdf setTitle(String title) { | ||
this.title = title; | ||
return this; | ||
} | ||
|
||
private byte[] getPdf(String html) { | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
try { | ||
InputStream input = new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)); | ||
ConverterProperties converterProperties = new ConverterProperties(); | ||
converterProperties.setFontProvider(new FontProvider()); | ||
converterProperties.getFontProvider().addFont("fonts/NotoSansCJKtc-Regular.otf"); | ||
converterProperties.getFontProvider().addFont("fonts/NotoSansCJKtc-Bold.otf"); | ||
HtmlConverter.convertToPdf(input, os, converterProperties); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return os.toByteArray(); | ||
} | ||
|
||
private String fixHtml(String html) { | ||
return | ||
"<!DOCTYPE html>\n<html>\n" + | ||
"<head><title>" + | ||
title + | ||
"</title></head>" + | ||
"<body>\n" + html + "\n</body></html>"; | ||
} | ||
|
||
private static String getMarkDownString(String dr, String dh, String ou) { | ||
String markdown = | ||
"# PDAS 個人資料授權系統\n" + | ||
"\n---\n\n" + | ||
"\n- 簽署人 (資料請求者):" + dr + | ||
"\n- 簽署人 (資料保管者):" + dh + | ||
(ou != null ? "\n- 簽署人 (授權人):" + ou : "") + | ||
"\n\n---\n\n" | ||
; | ||
|
||
final String contractBody = | ||
"# 資料授權同意書\n" + | ||
"\n" + | ||
"### 契約簽署人\n" + | ||
"- 資料請求者 (甲方):SOSELab Co.\n" + | ||
"- 資料保管者 (乙方):台灣電力公司\n" + | ||
"- 授權人 (丙方)\n" + | ||
"\n" + | ||
"### 資料授權範圍\n" + | ||
"- 目的:分析用電量,建立用電預測模型。\n" + | ||
"- 期間:不限。\n" + | ||
"- 地區:不限。\n" + | ||
"- 對象:SOSELab Co.\n" + | ||
"- 方式:自動化機器或非自動方式。\n" + | ||
"\n" + | ||
"*依個資法第三條規定,當事人可以行使以下權利,請求閱覽、副本、更正、停止處理利用、刪除。*\n" + | ||
"\n" + | ||
"### 權利以及關係\n" + | ||
"1. 甲方須提供權利金給資料所有人丙方以及資料保管人乙方。\n" + | ||
"2. 丙方同意授權上方載述之資料範圍給甲方利用。"; | ||
|
||
|
||
return markdown + contractBody; | ||
} | ||
public static void main(String[] args) { | ||
final String str = getMarkDownString("SOSELab Co.", "台灣電力公司", "Stanley"); | ||
try { | ||
Files.write(Paths.get("test.pdf"), new JavaMarkdownToPdf().convert(str)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |