1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.tanpu.community.util;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.*;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.ArrayList;
@Slf4j
public class PagePdfGenUtils {
private static Browser browser;
private static volatile boolean isBrowserClosed;
static {
try {
BrowserFetcher.downloadIfNotExist(null);
ArrayList<String> argList = new ArrayList<>();
LaunchOptions options = new LaunchOptionsBuilder()
.withExecutablePath("/usr/bin/google-chrome")
.withArgs(argList)
.withHeadless(true)
.build();
argList.add("--no-sandbox");
argList.add("--disable-setuid-sandbox");
argList.add("--enable-logging=/data/logs/chrome22.log --v=1");
browser = Puppeteer.launch(options);
// for (int i = 0;i < 5; i++) {
// log.info("##### start genPdf " + i);
// Thread t = new Thread(new Runnable() {
// @Override
// public void run() {
// try {
// genPdf("https://testtamper.tanpuyun.com/hangjiapc/#/billingPdf");
// } catch (Throwable e) {
// log.error("#### error", e);
// e.printStackTrace();
// }
// }
// });
// t.start();
// }
isBrowserClosed = false;
} catch (Throwable t) {
log.error("error in init genPdf", t);
}
}
public static void genPdf(String url) throws Throwable {
// ArrayList<String> argList = new ArrayList<>();
// LaunchOptions options = new LaunchOptionsBuilder()
// .withExecutablePath("/usr/bin/google-chrome")
// .withArgs(argList)
// .withHeadless(true)
// .build();
//
// argList.add("--no-sandbox");
// argList.add("--disable-setuid-sandbox");
// argList.add("--enable-logging=/data/logs/chrome.log");
// argList.add("--v=2");
// browser = Puppeteer.launch(options);
Page page = browser.newPage();
page.goTo(url);
PDFOptions pdfOptions = new PDFOptions();
pdfOptions.setPath("/usr/src/myapp/test.pdf");
pdfOptions.setPrintBackground(true);
byte[] pdfBytes = page.pdf(pdfOptions);
ScreenshotOptions screenshotOptions = new ScreenshotOptions();
//设置截图范围
Clip clip = new Clip(1.0,1.56,400,400);
screenshotOptions.setClip(clip);
screenshotOptions.setType("png");
//设置存放的路径
screenshotOptions.setPath("/usr/src/myapp/test.png");
page.screenshot(screenshotOptions);
page.close();
// browser.close();
}
}