如何在java中使用iText & 将html页面的url转换为pdf飞碟?

发布于 2024-09-28 14:44:46 字数 910 浏览 3 评论 0原文

我刚刚下载了 xhtmlrenderer 和 iText jar 文件。我可以使用这些 jar 来制作 pdf 文件。

我真正想要的是: 如果我在“inputFile”的位置给出一个有效的 URL(例如“https://xhtmlrenderer.dev.java.net/news.html”),我需要创建 pdf。飞碟和 iText 可以吗?

如果是,请指导我实现这一目标。

另外,当我尝试运行以下代码时,出现错误:流已关闭

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();
    }
}

I've just downloaded xhtmlrenderer and iText jar files. I can make pdf files by using these jars.

What I exactly want is:
I need to create pdf if I give one valid URL (say "https://xhtmlrenderer.dev.java.net/news.html") in the place of "inputFile". Is it possible with flying saucer and iText?

If yes, please guide me to achieve this.

Also, when I'm trying to run the below code, I'm getting error: 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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-10-05 14:44:46

是的...这可能不起作用,因为请求的页面不是 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”文件夹必须存在于您工作区的项目中或您运行应用程序的任何位置

Yes... this probably won't work as the page being requested isn't xhtml but this should do the trick:

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();
}
}

The stream closed error occurs when the file you're requesting isn't found. The 'samples' folder must exist in the project in your workspace or wherever it is that you're running your application from

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文