I think you can open you html file in Chrome. Then print it to pdf format. Then it will works.
That is when you print it, you choose "save as pdf" rather than your printer.
Answer from daizuozhuo on Stack OverflowVideos
i have html page i wants to convert it to a pdf file but i keep loosing the full page. i tried many tools but nothing working
I have been trying for the last couple days but have had no luck in figuring it out. Any help will be much appreciated
I used ITextRenderer from the Flying Saucer project.
Here is a short, self-contained, working example. In my case I wanted to later stream the bytes into an email attachment.
So, in the example I write it to a file purely for the sake of demonstration for this question. This is Java 8.
import com.lowagie.text.DocumentException;
import org.apache.commons.io.FileUtils;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class So4712641 {
public static void main(String... args) throws DocumentException, IOException {
FileUtils.writeByteArrayToFile(new File("So4712641.pdf"), toPdf("<b>You gotta walk and don't look back</b>"));
}
/**
* Generate a PDF document
* @param html HTML as a string
* @return bytes of PDF document
*/
private static byte[] toPdf(String html) throws DocumentException, IOException {
final ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
try (ByteArrayOutputStream fos = new ByteArrayOutputStream(html.length())) {
renderer.createPDF(fos);
return fos.toByteArray();
}
}
}
This gives me
For completeness, here are relevant pieces for my Maven pom.xml
<dependencies>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.0.8</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
Converting HTML to PDF isn't exactly straightforward in general, but if you're in control of what goes into the HTML, you can try using an XSL-FO implementation, like Apache FOP.
It's not out-of-the-box as you'll have to write (or find) a stylesheet that defines the conversion rules, but on the upside it gives you much more control over output formatting, which is quite useful as what looks good on screen doesn't necessarily look good on paper.