I have a service with a dependency to a document generation library. The library uses iText pdfHTML to convert HTML to PDF. Inside the document, the Table of contents contains links for jumping to specific sections of the document, as well as the page numbers. It was made via HTML bookmarks with ID attribute and links.
The links are working when the dependency points to my local repository. But when the dependency points to the remote repository, the links are no longer working. When mousing over the broken link, I see the text "jar:file:/.../printerlibrary-{version}.jar!/print-resource/#section-1". Page numbers are displayed correctly in both scenarios.
I also tried to generate bookmarks using this guide, but the same issue occurred.
I am using the latest version of pdfHTML (6.1.0)
/print-resource/example.html:
<html>
<head>
<style>
.new-page {
page-break-before: always;
}
ul.toc a::after {
content: target-counter(attr(href), page);
float: right;
}
</style>
</head>
<body>
<div id="section-1">
<h1>Section 1</h1>
<p>some content...</p>
</div>
<div class="new-page">
<h2 id="table-of-content">Table of content</h2>
<ul>
<li><a href="#section-1">Section 1</a></li>
<li><a href="#table-of-content">Table of content</a></li>
<li><a href="#section-2">Section 2</a></li>
<li><a href="#section-3">Section 3</a></li>
</ul>
</div>
<div class="new-page">
<h2 id="section-2">Section 2</h2>
<p>some content...</p>
</div>
<div class="new-page">
<h2 id="section-3">Section 3</h2>
<p>some content...</p>
</div>
</body>
</html>
Printer.java:
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();
}
}
}
ServiceImpl.java:
public class ServiceImpl implements Service {
private byte[] print() {
return Printer.printAsPDF();
}
}
1 Answer 1
Hmm on Windows this is a one-line native ComManD (no need for JavaScript or iText) since the HTML is printed to PDF and any <h#> is treated as an outline object. Here we see them outlined in blue.
In any other Mac Linux system where Chrome-headless-shell is supported the same can be done by using that in the same way.
The one line of concatenated programming is:
start msedge --headless --no-pdf-header-footer --run-all-compositor-stages-before-draw --virtual-time-budget=80000 --generate-pdf-document-outline --print-to-pdf="%cd%\filename.pdf" "filename.html"
Depending on versions you may need to adjust a few arguments parameters such as:
start msedge --headless=new --log-level=3 --user-data-dir="%temp%\temp" --no-pdf-header-footer ..................
In Edge itself Powered by Acrobat the hyperlinks are shown underlined in blue.
Comments
Explore related questions
See similar questions with these tags.