2013-04-24 10 views
5

È possibile eseguire il rendering di documenti PDF con Play Framework 2?Play Framework 2 Render pdf

(c'è un modulo che può rendere pdf per il gioco 1.x. c'è un modo per rendere in gioco 2?)

risposta

5

Se stai cercando di rendere modelli di vista come documenti PDF, controlla this module .

+0

sì che cercavo questo. controllerò :) – zeal

+0

Non è più supportato. Hai un'idea per sostituire questo modulo? – Moebius

+2

Ciao @Moebius, abbiamo fatto un fork con la compatibilità 2.3.x https://github.com/innoveit/play2-pdf – MaFo

0

C'è il plugin apache fop che crea pdf dai file fop.

I file fop non sono i file più intuitivi, ma alla fine ho sempre trovato un modo per formattare il pdf complesso nel modo in cui lo volevo.

Per aggiungere il plug-in per l'applicazione gioco per aggiungere questo build.sbt:

"org.apache.avalon.framework" % "avalon-framework-api" % "4.2.0" from "http://repo1.maven.org/maven2/avalon-framework/avalon-framework-api/4.2.0/avalon-framework-api-4.2.0.jar", 
"org.apache.avalon.framework" % "avalon-framework-impl" % "4.2.0" from "http://repo1.maven.org/maven2/avalon-framework/avalon-framework-impl/4.2.0/avalon-framework-impl-4.2.0.jar", 
"org.apache.xmlgraphics" % "fop" % "1.1" 

Questa è la mia funzione per creare un file PDF da una stringa FOP:

private static FopFactory fopFactory = FopFactory.newInstance(); 

/** 
* Wrote according to this example : 
* http://xmlgraphics.apache.org/fop/1.1/embedding.html#examples 
* @param outputPath Path to the file to create (must end by .pdf). 
* @param foString  Description of the pdf document to render. 
*      http://www.w3schools.com/xslfo/default.asp 
* @return the output path. 
*/ 
public static String toPdf(String outputPath, String foString) 
{ 
    OutputStream out; 
    try { 
     File fileOutput = new File(outputPath); 
     out = new BufferedOutputStream(new FileOutputStream(fileOutput)); 
    } catch (FileNotFoundException e) { 
     Logger.error("InvoicePdf.invoiceToPdf: " + e.getMessage()); 
     return null; 
    } 
    try { 
     Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); 
     TransformerFactory factory = TransformerFactory.newInstance(); 
     Transformer transformer = factory.newTransformer(); 
     Source src = new StreamSource(new StringReader(foString)); 
     Result res = new SAXResult(fop.getDefaultHandler()); 
     transformer.transform(src, res); 
    }catch (Throwable e){ 
     Logger.error("InvoicePdf.invoiceToPdf: " + e.getMessage()); 
     e.printStackTrace(); 
     return null; 
    } finally { 
     try { 
      out.close(); 
     } catch (Throwable e) { 
      Logger.error("InvoicePdf.invoiceToPdf: " + e.getMessage()); 
     } 
    } 
    return outputPath; 
} 
0

Quando si utilizza giocare con scala è possibile utilizzare la libreria di scala https://github.com/cloudify/sPDF.

Poi nel controller gioco 2.x si può rendere il pdf con il seguente codice:

import io.github.cloudify.scala.spdf.{Pdf, PdfConfig, Portrait} 

def yourAction = Action { implicit request => 
    val pdf = Pdf(
    executablePath = "/usr/bin/wkhtmltopdfPath", 
    config = new PdfConfig { 
     orientation := Portrait 
     pageSize := "A4" 
     marginTop := "0.5in" 
     marginBottom := "0.5in" 
     marginLeft := "0.5in" 
     marginRight := "0.5in" 
     printMediaType := Some(true) 
    } 
) 

    val outputStream = new ByteArrayOutputStream 

    pdf.run(
    sourceDocument = views.html.yourTemplate().toString(), 
    destinationDocument = outputStream 
) 

    Ok(outputStream.toByteArray).as("application/pdf") 
}