Search code examples
javaimagepdfitextpdfbox

Create a PDF file from HTML specified via URL


I would like to generate a PDF and Image file from HTML specified via URL(page) and send it to an external system. I need to perform this generation logic in java. The most popular and widely used libraries for generating PDFs from HTML are Apache PDFBox and iText. But is there any tool available to generate PDF and image based on the page URL?


Solution

  • You can use Flying Saucer: Flying Saucer is a Java library that can convert XHTML and HTML documents to PDF and images. It can render web pages, including external resources, and convert them into PDFs or images.

    GitHub Repository: https://github.com/flyingsaucerproject/flyingsaucer

    Here's a basic example using Flying Saucer to convert HTML to a PDF in Java:

    import org.xhtmlrenderer.pdf.ITextRenderer;
    
    public class HtmlToPdfConverter {
        public static void main(String[] args) throws Exception {
            String url = "http://example.com"; // Replace with your desired URL
            String outputPdfFile = "output.pdf";
    
            // Create an ITextRenderer instance
            ITextRenderer renderer = new ITextRenderer();
    
            // Set the URL to be converted
            renderer.setDocument(url);
    
            // Perform the layout and render to PDF
            renderer.layout();
            renderer.createPDF(outputPdfFile);
    
            // Close the renderer
            renderer.finishPDF();
        }
    }
    

    Please note that you'll need to handle exceptions, configure options, and adapt the code to your specific requirements, such as adding error handling, specifying output paths, and adjusting rendering settings.

    edit To generate image you can refer below code snippet

    import org.xhtmlrenderer.simple.Graphics2DRenderer;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    
    public class HtmlToImageConverter {
        public static void main(String[] args) throws Exception {
            String url = "http://example.com"; // Replace with your desired URL
            String outputImageFile = "output.png";
    
            // Create a Graphics2DRenderer instance
            Graphics2DRenderer renderer = new Graphics2DRenderer();
    
            // Set the URL to be converted
            renderer.setDocument(url);
    
            // Perform layout
            renderer.layout();
    
            // Create a BufferedImage
            BufferedImage image = new BufferedImage(renderer.getWidth(),
                                                      renderer.getHeight(),
                                                      BufferedImage.TYPE_INT_RGB);
    
            // Render HTML content to the BufferedImage
            renderer.createPDF(image.createGraphics(), false);
    
            // Save the BufferedImage as an image file
            File outputFile = new File(outputImageFile);
            ImageIO.write(image, "png", outputFile);
    
            System.out.println("Image saved to: " + outputFile.getAbsolutePath());
        }
    }