繁体   English   中英

Play Framework 2渲染pdf

[英]Play Framework 2 Render pdf

是否可以使用play framework 2渲染pdf文档?

(有一个可以为播放1.x渲染pdf的模块。有没有一种方法可以在播放2中渲染?)

如果您希望将视图模板渲染为PDF文档,请查看此模块

有一个Apache FOP插件,可以从FOP文件中创建pdf。

fop文件不是最直观的文件,但最终,我总是找到了一种以所需方式格式化复杂pdf的方法。

要将插件添加到您的播放应用程序中,请将其添加到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"

这是我从fop字符串创建pdf文件的功能:

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

在scala中使用play时,可以使用scala库https://github.com/cloudify/sPDF

然后在Play 2.x控制器中,您可以使用以下代码来渲染pdf:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM