2013-07-24 45 views
16
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PdfWriter; 

public class GeneratePDF { 
    public static void main(String[] args) { 
     try { 

      String k = "<html><body> This is my Project </body></html>"; 

      OutputStream file = new FileOutputStream(new File("E:\\Test.pdf")); 

      Document document = new Document(); 
      PdfWriter.getInstance(document, file); 

      document.open(); 

      document.add(new Paragraph(k)); 

      document.close(); 
      file.close(); 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 
    } 
} 

Đây là mã của tôi để chuyển đổi HTML sang PDF. Tôi có thể chuyển đổi nó nhưng trong tập tin PDF nó tiết kiệm như HTML toàn bộ trong khi tôi cần phải hiển thị chỉ văn bản. <html><body> This is my Project </body></html> được lưu vào PDF trong khi nó chỉ nên lưu This is my Project.Cách chuyển đổi HTML sang PDF bằng cách sử dụng iText

+0

thử này http://stackoverflow.com/questions/4712641/java-how-to-convert-a-html -content-to-pdf-without-loss-the-format – pratZ

+0

Tôi cần chuyển đổi Trong java –

+0

[API mở văn phòng Apache] (http://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#How_to_use_it_from_Java) –

Trả lời

39

Bạn có thể làm điều đó với lớp HTMLWorker (không được chấp nhận) như sau:

import com.itextpdf.text.html.simpleparser.HTMLWorker; 
//... 
try { 
    String k = "<html><body> This is my Project </body></html>"; 
    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf")); 
    Document document = new Document(); 
    PdfWriter.getInstance(document, file); 
    document.open(); 
    HTMLWorker htmlWorker = new HTMLWorker(document); 
    htmlWorker.parse(new StringReader(k)); 
    document.close(); 
    file.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

hoặc sử dụng XMLWorker, (tải về từ this jar) sử dụng mã này:

import com.itextpdf.tool.xml.XMLWorkerHelper; 
//... 
try { 
    String k = "<html><body> This is my Project </body></html>"; 
    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf")); 
    Document document = new Document(); 
    PdfWriter writer = PdfWriter.getInstance(document, file); 
    document.open(); 
    InputStream is = new ByteArrayInputStream(k.getBytes()); 
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); 
    document.close(); 
    file.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+1

Nhưng lớp Công nhân Html không làm việc nó không được chấp nhận vì vậy bạn có thể vui lòng cho tôi biết tệp jar nào chúng tôi cần cho Html Workerclass không? –

+0

thấy cập nhật của tôi vào nhập khẩu – MaVRoSCy

+0

Nhưng vẫn Đây là dự án của tôi này hiển thị trong Pdf trong khi tôi cần phải hiển thị chỉ này dự án của tôi chỉ bên trong văn bản của html –

1

Liên kết này có thể hữu ích khi chuyển đổi.

https://code.google.com/p/flying-saucer/

https://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html

Nếu nó là một dự án đại học, thậm chí bạn có thể đi cho các, http://pd4ml.com/examples.htm

Ví dụ được đưa ra để chuyển đổi HTML sang PDF

+0

những gì jar tập tin chúng ta cần phải thêm vào dự án của tôi? –

+0

Bạn sẽ nhận được trợ giúp đó trong liên kết, Vẫn truy cập liên kết này, sao chép zip, lấy jar, đặt trong đường dẫn: https://code.google.com/p/flying-saucer/downloads/list – Jayesh

Các vấn đề liên quan