2012-05-08 12 views
9

Tengo que ofrecer algunas funciones de exportación a mi sitio web, como CSV o PDF. ¿Existe una herramienta poderosa y gratuita para Java para convertir páginas HTML a formato PDF?¿Cómo exportar la página html al formato pdf?

+1

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

Respuesta

27

Usando Flying Saucer API con iText PDF se puede convertir el contenido HTML a PDF.
Los siguientes ejemplos lo ayudan a comprender, en cierta medida, la conversión de XHTML a PDF.

Ejemplos de uso de la API de platillo volante:
Necesita siguientes bibliotecas:

  • núcleo renderer.jar
  • iText-2.0.8.jar

Usted puede encontrar estos recursos en flyingsaucer-R8.zip.

Ejemplo 1: Uso de Recursos 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."); 

Ejemplo 2: Uso de la introducción directa XHTML 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."); 

Ejemplos Usando iText API:
Necesita siguientes bibliotecas :

  • núcleo renderer.jar
  • itextpdf-5.2.1.jar

Usted puede encontrar estos recursos en here.

Ejemplo 3: El uso de HTML Trabajador:

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

pena por una respuesta tardía .. ... htmlWorker.parse (nuevo StringReader (sb.toString())); Obtengo una salida JSP ... ¿cómo la convierto a HTML? Creo que sb es el contenido XHTML/HTML. ¿Está bien? – Harry

+0

letras árabes no se muestran con su código. Hay alguna manera ? – user4757345

+0

@ user4757345: ¿Has visto que el tipo de contenido '' html'' está configurado como '" text/html; charset = utf-8 "'. Básicamente es la instrucción HTML para que el analizador comprenda y use la misma al crear la página PDF. –