2012-05-08 22 views
9

Devo offrire alcune funzioni di esportazione al mio sito Web come CSV o PDF. C'è uno strumento potente e gratuito per Java per convertire le pagine HTML in formato PDF?Come esportare la pagina html in formato pdf?

+1

Eventuali duplicati: http://stackoverflow.com/questions/633780/converting-html-files-to-pdf – David

risposta

27

Utilizzando Flying Saucer API con iText PDF è possibile convertire i contenuti HTML in PDF.
Gli esempi seguenti consentono di comprendere, in una certa misura, la conversione di XHTML in PDF.

Esempi di utilizzo Flying Saucer API:
Si richiedono seguenti librerie:

  • core-renderer.jar
  • iText-2.0.8.jar

È possibile trovare questi risorse in flyingsaucer-R8.zip.

Esempio 1: Utilizzo di risorse XML:

// if you have html source in hand, use it to generate document object 
Document document = XMLResource.load(new ByteArrayInputStream(yourXhtmlContentAsString.getBytes())).getDocument(); 

ITextRenderer renderer = new ITextRenderer(); 
renderer.setDocument(document, null); 

renderer.layout(); 

String fileNameWithPath = outputFileFolder + "PDF-XhtmlRendered.pdf"; 
FileOutputStream fos = new FileOutputStream(fileNameWithPath); 
renderer.createPDF(fos); 
fos.close(); 
System.out.println("File 1: '" + fileNameWithPath + "' created."); 

Esempio 2: Uso del metodo di diretta Valid al documento:

ITextRenderer renderer = new ITextRenderer(); 

// if you have html source in hand, use it to generate document object 
renderer.setDocumentFromString(yourXhtmlContentAsString); 
renderer.layout(); 

String fileNameWithPath = outputFileFolder + "PDF-FromHtmlString.pdf"; 
FileOutputStream fos = new FileOutputStream(fileNameWithPath); 
renderer.createPDF(fos); 
fos.close(); 

System.out.println("File 2: '" + fileNameWithPath + "' created."); 

esempi utilizzando iText API:
Si richiedono seguenti librerie :

  • core-renderer.jar
  • itextpdf-5.2.1.jar

È possibile trovare queste risorse a here.

Esempio3: Uso di HTML Worker:

com.itextpdf.text.Document document = 
     new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4); 
String fileNameWithPath = outputFileFolder + "PDF-HtmlWorkerParsed.pdf"; 
FileOutputStream fos = new FileOutputStream(fileNameWithPath); 
com.itextpdf.text.pdf.PdfWriter pdfWriter = 
     com.itextpdf.text.pdf.PdfWriter.getInstance(document, fos); 

document.open(); 

//********************************************************** 
// if required, you can add document meta data 
document.addAuthor("Ravinder"); 
//document.addCreator(creator); 
document.addSubject("HtmlWoker Parsed Pdf from iText"); 
document.addCreationDate(); 
document.addTitle("HtmlWoker Parsed Pdf from iText"); 
//**********************************************************/ 

com.itextpdf.text.html.simpleparser.HTMLWorker htmlWorker = 
     new com.itextpdf.text.html.simpleparser.HTMLWorker(document); 
htmlWorker.parse(new StringReader(sb.toString())); 

document.close(); 
fos.close(); 

System.out.println("File 3: '" + fileNameWithPath + "' created."); 
+0

dispiace per la risposta tardiva .. ... htmlWorker.parse (new StringReader (sb.toString())); Ricevo un output JSP ... come posso convertirlo in HTML. Penso che sb sia il contenuto XHTML/HTML. È giusto? – Harry

+0

Le lettere arabe non sono mostrate con il tuo codice. C'è un modo? – user4757345

+0

@ user4757345: hai visto che il tipo di contenuto ''html'' è impostato come' "text/html; charset = utf-8" '. Fondamentalmente è l'istruzione HTML in modo che il parser capisca e usi lo stesso mentre costruisce la pagina PDF. –

Problemi correlati