Skip to content

Instantly share code, notes, and snippets.

@MikeThomsen
Created September 29, 2020 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeThomsen/387564dc746e13b305e7e52f9c5ba4c6 to your computer and use it in GitHub Desktop.
Save MikeThomsen/387564dc746e13b305e7e52f9c5ba4c6 to your computer and use it in GitHub Desktop.
Generate a PDF from HTML
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.apache.pdfbox.io.IOUtils;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
String html = new String(IOUtils.toByteArray(App.class.getResourceAsStream("/test.html")));
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withHtmlContent(html, "/root.html");
builder.useFastMode();
builder.toStream(os);
builder.run();
os.close();
FileOutputStream out = new FileOutputStream("Test.pdf");
out.write(os.toByteArray());
out.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment