main
黄海 10 months ago
parent 22e6006abe
commit 50b3c6a900

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$/dsPay" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/dsPay/src/resources" type="java-resource" />
</content>
</component>
</module>

@ -42,7 +42,11 @@
<artifactId>xhtmlrenderer</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.icepear.echarts</groupId>
<artifactId>echarts-java</artifactId>
<version>1.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>

@ -0,0 +1,26 @@
import org.icepear.echarts.Bar;
import org.icepear.echarts.render.Engine;
public class Echarts {
//https://github.com/ECharts-Java/ECharts-Java
public static void main(String[] args) {
// All methods in EChart Java supports method chaining
Bar bar = new Bar()
.setLegend()
.setTooltip("item")
.addXAxis(new String[] { "Matcha Latte", "Milk Tea", "Cheese Cocoa", "Walnut Brownie" })
.addYAxis()
.addSeries("2015", new Number[] { 43.3, 83.1, 86.4, 72.4 })
.addSeries("2016", new Number[] { 85.8, 73.4, 65.2, 53.9 })
.addSeries("2017", new Number[] { 93.7, 55.1, 82.5, 39.1 });
Engine engine = new Engine();
// The render method will generate our EChart into a HTML file saved locally in the current directory.
// The name of the HTML can also be set by the first parameter of the function.
engine.render("index.html", bar);
// It is recommended that you can get the serialized version of Option in the representation of JSON, which can be used directly in the template or in the RESTful APIs.
String jsonStr = engine.renderJsonOption(bar);
System.out.println(jsonStr);
}
}

@ -1,82 +0,0 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
public class HtmltoPDF {
public static void main( String[] args ) throws Exception{
// htmlToPdf();
// htmlToPdf2();
htmlToPdf3();
}
// 不支持中文
public static void htmlToPdf() throws Exception
{
String inputFile = "D:/Test/flying.html";
String url = new File( inputFile ).toURI().toURL().toString();
String outputFile = "D:/Test/flying.pdf";
OutputStream os = new FileOutputStream( outputFile );
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument( url );
renderer.layout();
renderer.createPDF( os );
os.close();
}
// 支持中文
public static void htmlToPdf2() throws Exception {
String outputFile = "F:/TestDemo/demo_3.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
StringBuffer html = new StringBuffer();
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").
append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")
.append("</head>")
.append("<body>");
html.append("<div>支持中文!</div>");
html.append("</body></html>");
renderer.setDocumentFromString(html.toString());
// 解决图片的相对路径问题
// renderer.getSharedContext().setBaseURL("file:/F:/teste/html/");
renderer.layout();
renderer.createPDF(os);
System.out.println("======转换成功!");
os.close();
}
public static void htmlToPdf3() throws Exception{
String inputFile = "F:/TestDemo/test.html"; //本地目录必须html语言否则报错
String outFile = "F:/TestDemo/test.pdf";
OutputStream os = null;
os = new FileOutputStream(outFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
String url = new File( inputFile ).toURI().toURL().toString();
System.out.println("=============url: "+url);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
System.out.println("======转换成功!");
os.close();
}
}

@ -0,0 +1,70 @@
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>ECharts Demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.2.2/echarts.min.js"
integrity="sha512-ivdGNkeO+FTZH5ZoVC4gS4ovGSiWc+6v60/hvHkccaMN2BXchfKdvEZtviy5L4xSpF8NPsfS0EVNSGf+EsUdxA=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
body {
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
}
#title-container {
width: 600px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
#download-btn {
border-radius: 6px;
background-color: aliceblue;
border: 1px solid black;
}
#display-container {
width: 600px;
height: 600px;
border: 2px solid black;
}
</style>
</head>
<body>
<div id="container">
<div id="title-container">
<h1 class="display">ECharts Java</h1>
<button id="download-btn" onclick="downloadImage()">Download Image</button>
</div>
<div id="display-container">
</div>
</div>
<script type="text/javascript">
function downloadImage() {
let canvas = document.querySelector("canvas");
let image = canvas.toDataURL("image/png").replace("image/png",
"image/octet-stream");
let fakeLink = document.createElement("a");
if (typeof fakeLink.download === "string") {
document.body.appendChild(fakeLink);
fakeLink.download = "echart.png";
fakeLink.href = image;
fakeLink.click();
document.body.removeChild(fakeLink);
} else {
location.replace(image);
}
}
var chart = echarts.init(document.getElementById("display-container"));
var option = {"xAxis":[{"type":"category","data":["Matcha Latte","Milk Tea","Cheese Cocoa","Walnut Brownie"]}],"yAxis":[{"type":"value"}],"tooltip":{"trigger":"item"},"legend":{},"series":[{"type":"bar","name":"2015","data":[43.3,83.1,86.4,72.4]},{"type":"bar","name":"2016","data":[85.8,73.4,65.2,53.9]},{"type":"bar","name":"2017","data":[93.7,55.1,82.5,39.1]}]}
chart.setOption(option);
</script>
</body>
</html>
Loading…
Cancel
Save