Play Framework 2 Render pdf

Can I make a PDF using Playback Platform 2?

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

+5
source share
3 answers

If you want to visualize presentation templates as PDF documents, check out this module .

+5
source

There is an apache fop plugin that creates a pdf file from fop files.

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.

To add the plug-in to the playback application, add it 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"

pdf 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

scala scala https://github.com/cloudify/sPDF.

Then in your Play 2.x controller you can make a 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")
}
0
source

All Articles