Code: Select all
/print-resource/example.html
Code: Select all
.new-page {
page-break-before: always;
}
ul.toc a::after {
content: target-counter(attr(href), page);
float: right;
}
Section 1
some content...
Table of content
[list]
[*][url=#section-1]Section 1[/url]
[*][url=#table-of-content]Table of content[/url]
[*][url=#section-2]Section 2[/url]
[*][url=#section-3]Section 3[/url]
[/list]
Section 2
some content...
Section 3
some content...
< /code>
Printer.java
Code: Select all
public class Printer {
protected byte[] printAsPDF() throws IOException {
String html = "...";
URL resource = getClass().getClassLoader().getResource("print-resource");
String resourcePath = resource.toExternalForm();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
PdfWriter pdfWriter = new PdfWriter(outputStream, new WriterProperties());
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
ConverterProperties props = new ConverterProperties();
props.setBaseUri(resourcePath);
HtmlConverter.convertToPdf(html, pdfDocument, props);
return outputStream.toByteArray();
}
}
}
< /code>
ServiceImpl.java
Code: Select all
public class ServiceImpl implements Service {
private byte[] print() {
return Printer.printAsPDF();
}
}