Play Framework 2 Render pdf

时间秒杀一切 提交于 2019-12-23 12:13:12

问题


Is it possible to render pdf document with play framework 2?

(There is a module that can render pdf for play 1.x. is there a way to render in play 2?)


回答1:


If you're looking to render view templates as PDF documents, check out this module.




回答2:


There is the apache fop plugin which create pdf out of fop files.

The fop files are not the most intuitive files, but in the end I always found a way to format complex pdf the way I wanted it.

To add the plug in to your play application add this to 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"

This is my function to create a pdf file from a fop string :

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;
}



回答3:


When using play with scala you can use the scala library https://github.com/cloudify/sPDF.

Then in your Play 2.x controller you can render pdf with the following code:

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")
}


来源:https://stackoverflow.com/questions/16188374/play-framework-2-render-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!