Skip to content

Commit 1d4f012

Browse files
committed
Add print-to-pdf example
1 parent 671d2dc commit 1d4f012

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.github.kklisura.cdt.examples;
2+
3+
import com.github.kklisura.cdt.launch.ChromeLauncher;
4+
import com.github.kklisura.cdt.protocol.commands.Page;
5+
import com.github.kklisura.cdt.services.ChromeDevToolsService;
6+
import com.github.kklisura.cdt.services.ChromeService;
7+
import com.github.kklisura.cdt.services.types.ChromeTab;
8+
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.util.Base64;
12+
13+
/**
14+
* The following example opens the http://github.com and prints its page to PDF. PDF printing is supported on chrome
15+
* headless at the moment.
16+
*
17+
* @author Kenan Klisura
18+
*/
19+
public class PrintingPageToPdf {
20+
public static void main(String[] args) {
21+
// Create chrome launcher.
22+
final ChromeLauncher launcher = new ChromeLauncher();
23+
24+
// Launch chrome either as headless (true) - PDF printing is only supported on Chrome headless at the moment
25+
final ChromeService chromeService = launcher.launch(true);
26+
27+
// Create empty tab ie about:blank.
28+
final ChromeTab tab = chromeService.createTab();
29+
30+
// Get DevTools service to this tab
31+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
32+
33+
// Get individual commands
34+
final Page page = devToolsService.getPage();
35+
page.enable();
36+
37+
// Navigate to github.com.
38+
page.navigate("http://github.com");
39+
40+
page.onLoadEventFired(loadEventFired -> {
41+
System.out.println("Printing to PDF...");
42+
43+
final String outputFilename = "test.pdf";
44+
45+
Boolean landscape = false;
46+
Boolean displayHeaderFooter = false;
47+
Boolean printBackground = false;
48+
Double scale = 1d;
49+
Double paperWidth = 8.27d; // A4 paper format
50+
Double paperHeight = 11.7d; // A4 paper format
51+
Double marginTop = 0d;
52+
Double marginBottom = 0d;
53+
Double marginLeft = 0d;
54+
Double marginRight = 0d;
55+
String pageRanges = "";
56+
Boolean ignoreInvalidPageRanges = false;
57+
String headerTemplate = "";
58+
String footerTemplate = "";
59+
Boolean preferCSSPageSize = false;
60+
61+
dump(outputFilename,
62+
devToolsService.getPage().printToPDF(
63+
landscape,
64+
displayHeaderFooter,
65+
printBackground,
66+
scale,
67+
paperWidth,
68+
paperHeight,
69+
marginTop,
70+
marginBottom,
71+
marginLeft,
72+
marginRight,
73+
pageRanges,
74+
ignoreInvalidPageRanges,
75+
headerTemplate,
76+
footerTemplate,
77+
preferCSSPageSize)
78+
);
79+
80+
System.out.println("Done!");
81+
devToolsService.close();
82+
});
83+
84+
devToolsService.waitUntilClosed();
85+
}
86+
87+
private static void dump(String fileName, String data) {
88+
FileOutputStream fileOutputStream = null;
89+
try {
90+
File file = new File(fileName);
91+
fileOutputStream = new FileOutputStream(file);
92+
fileOutputStream.write(Base64.getDecoder().decode(data));
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
} finally {
96+
if (fileOutputStream != null) {
97+
try {
98+
fileOutputStream.flush();
99+
fileOutputStream.close();
100+
} catch (IOException e) {
101+
e.printStackTrace();
102+
}
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)