Created
September 29, 2020 13:58
-
-
Save MikeThomsen/387564dc746e13b305e7e52f9c5ba4c6 to your computer and use it in GitHub Desktop.
Generate a PDF from HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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