How to convert html page url to pdf in java using iText and flying saucer?

I just uploaded the xhtmlrenderer and iText jar files. I can make PDF files using these banks.

I definitely want: I need to create a PDF if I give one valid URL (for example, " https://xhtmlrenderer.dev.java.net/news.html") instead of "inputFile". Is this possible with a flying saucer and iText?

If yes, please help me with this.

Also, when I try to run the code below, I get an error message: stream closed

import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class FirstDoc {

    public static void main(String[] args) 
            throws IOException, DocumentException {
        String inputFile = "samples/sql.html";
        String url = new File(inputFile).toURI().toURL().toString();
        String outputFile = "firstdoc.pdf";
        OutputStream os = new FileOutputStream(outputFile);

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);
        renderer.layout();
        renderer.createPDF(os);

        os.close();
    }
}
+3
source share
1 answer

... , , , xhtml, :

import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class FirstDoc {

public static void main(String[] args) 
        throws IOException, DocumentException {
    String url= "http://xhtmlrenderer.java.net/news.html";

    String outputFile = "firstdoc.pdf";
    OutputStream os = new FileOutputStream(outputFile);

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(url);
    renderer.layout();
    renderer.createPDF(os);

    os.close();
}
}

, , , . 'samples'

0

Source: https://habr.com/ru/post/1771293/


All Articles