简体   繁体   English

如何将html页面的内容转成pdf

[英]How to the Get content of html page into pdf

I am trying to get or download the content of my html page that contains a lot of information(tables..) into pdf file.我正在尝试获取或下载包含大量信息(表格 ..)的 html 页面的内容到 pdf 文件中。 i try using this code but the got me the information as html information(with tags).我尝试使用此代码,但我得到的信息是 html 信息(带标签)。

Q : How to do get this content into a pdf?:如何将此内容转换为 pdf? here is my code :这是我的代码:

try{
    File file = new File("C:/Users/Hamza Amrani/Desktop/riad/testFile1.text");
    FileWriter writer_f = new FileWriter(file);
    URL url = new URL("http://localhost:8090/afficher?id=3");
      BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
      String line;

      while ((line = reader.readLine()) != null) {
         System.out.println(line);
         writer_f.write(line);
      }
      reader.close();
      writer_f.close();
    }
    catch (Exception e) {
        e.printStackTrace();
}

First of all your question is not clear.首先你的问题不是很清楚。 If you want to write a full a html page in pdf, then the code for getting html contents from URL is correct .如果你想用 pdf 写一个完整的 html 页面,那么从URL获取 html 内容的代码是正确的。 But for writing this contents in a pdf file, you will need iText , a Java library which allows to create PDF, read PDF and manipulate them.但是要将这些内容写入 pdf 文件,您将需要iText ,这是一个允许创建 PDF、阅读 PDF 和操作它们的 Java 库。 You can follow this tutorials to understand about iText :您可以按照本教程了解iText

But if you need to parse a HTML page like getting information inside a <h1> tag, you will need a HTML Parser .但是如果你需要解析一个HTML页面,比如在<h1>标签中获取信息,你将需要一个HTML Parser jsoup is the best solution for this problem. jsoup是这个问题的最佳解决方案。 Follow this links for better understanding about jsoup :按照此链接更好地了解jsoup

Hope this will solve your problem.希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM