简体   繁体   English

将HTML或GSP渲染为PDF并将其保存在服务器上

[英]Render HTML or GSP as a PDF and save it on server

I have an html template which I need to render as a .PDF and then save that pdf file on server. 我有一个html模板,需要将其渲染为.PDF,然后将该pdf文件保存在服务器上。 I'm using "rendering" plugin of grails. 我正在使用grails的“渲染”插件。 I'm able to render file as PDF but I don't understand how to save it on server location and not on user's system. 我可以将文件呈现为PDF,但不了解如何将其保存在服务器位置而不是用户系统上。 Can anybody please help me ? 有人可以帮我吗?

The pdfRenderingService provided by the plugin allows you to call render and get back an OutputStream. 插件提供的pdfRenderingService允许您调用render并获取OutputStream。 Using that output stream you can write that to a file on your server. 使用该输出流,您可以将其写入服务器上的文件。 The documentation explains the basics of using the service. 文档说明了使用该服务的基础知识。

Your code may look something like this: 您的代码可能看起来像这样:

new File("report.pdf").withOutputStream { outputStream ->
            outputStream << pdfRenderingService.render(template: '/report/report', model: [serial: 12345])
        }

Well, actually I changed my plugin. 好吧,实际上我更改了插件。 Got Wkhtmltopdf plugin of grails more helpful. 获得Wkhtmltopdf的grails插件更有用。 you can find it here -- https://github.com/quorak/grails-wkhtmltopdf 您可以在这里找到它-https: //github.com/quorak/grails-wkhtmltopdf

Also instructions regarding using this plugin you can find on the same link or here -- 另外,有关使用此插件的说明,您可以在同一链接或此处找到-

[https://github.com/quorak/grails-wkhtmltopdf] [https://github.com/quorak/grails-wkhtmltopdf]

Using this you can get "bytes" which you can write to file system. 使用它,您可以获得“字节”,可以将其写入文件系统。

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

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