プレーフレームワーク2でpdfドキュメントをレンダリングすることは可能ですか?Play Framework 2レンダリングpdf
(プレイ1.xのためにPDFファイルをレンダリングすることができ、モジュールが劇2でレンダリングする方法がありますか?)
プレーフレームワーク2でpdfドキュメントをレンダリングすることは可能ですか?Play Framework 2レンダリングpdf
(プレイ1.xのためにPDFファイルをレンダリングすることができ、モジュールが劇2でレンダリングする方法がありますか?)
あなたはPDF文書としてビューテンプレートをレンダリングするために探している場合は、チェックアウトthis module 。
fopファイルからpdfを作成するapache fopプラグインがあります。
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;
}
をあなたはスカラーライブラリhttps://github.com/cloudify/sPDFを使うことができます。はい私はそれを探していた
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")
}
:
は、その後、あなたのプレイ2.xのコントローラに次のコードを使用してPDFファイルをレンダリングすることができます。私はチェックアウトします:) – zeal
これはサポートされなくなりました。このモジュールを交換するアイデアはありましたか? – Moebius
こんにちは@Moebius、我々は2.3.xの互換性へのフォークを作りましたhttps://github.com/innoveit/play2-pdf – MaFo