gpt4 book ai didi

grails - Grails渲染插件保存到文件

转载 作者:行者123 更新时间:2023-12-02 13:54:06 24 4
gpt4 key购买 nike

显示 Controller 生成PDF的 Action 时,我正在尝试使用渲染插件将生成的pdf保存到文件中。我正在按照以下说明进行操作:http://gpc.github.com/grails-rendering/docs/manual/index.html

def pdf = {
def project = Project.get(params.id)
def numGoodMilestones = queryService.getGoodShapeMilestonesCount(project)
def totalMilestones = project.milestones.size()
def updateHistory = queryService.getReadableHistory(project)
def summaryName = "${project.name.replace(" ","_")}_summary_${String.format('%tF', new Date()).replace(" ","_")}"
if(!project)
{
flash.message = g.message(code:'default.not.found.message',
args:[message(code:'project.label',default:'Project'),params.id])
redirect(uri:'/')
}
// see if a summary has been generated with this data and attached to the
// project. If not, do it.
def existingAttachedSummary = ProjectDocument.findByName(summaryName)
if(!existingAttachedSummary)
{
//make the file
def savedSummary = new File(summaryName).withOutputStream { outputStream ->
pdfRenderingService.render( controller:this,
template: "projectDetail",
model:[project:project,
numGoodMilestones:numGoodMilestones,
totalMilestones:totalMilestones,
updateHistory: updateHistory])
}
def projectDocument = new ProjectDocument(name:summaryName,
description:"Project summary automatically generated on ${new Date()}}",
fileData:savedSummary,
owner: springSecurityService.currentUser,
project:project
)
if(projectDocument.validate())
{
projectDocument.save(flush:true)
flash.message="I saved a document, yo. ${projectDocument}."
}
else
{
flash.message="Errors, yo. ${projectDocument.errors.allErrors.each{ it }}."
}
}
else
{
flash.message = "project summary already attached to project"
}

renderPdf(template: "projectDetail",
model:[project:project, numGoodMilestones:numGoodMilestones, totalMilestones:totalMilestones, updateHistory: updateHistory],
filename: "${summaryName}.pdf")
}

renderPdf()方法工作正常,因为浏览器中的输出符合预期。但是,当我查看创建的ProjectDocument时,会看到一个空白的PDF文件。我试图以与渲染文档所述完全相同的方式保存到文件。我究竟做错了什么?

最佳答案

我认为这是文档中的错误。将outputStream作为最后一个参数传递给pdfRenderingService.render

def savedSummary = new File(summaryName).withOutputStream { outputStream ->
pdfRenderingService.render( controller:this,
template: "projectDetail",
model:[project:project,
numGoodMilestones:numGoodMilestones,
totalMilestones:totalMilestones,
updateHistory: updateHistory],
outputStream) // <- added this parameter
}

关于grails - Grails渲染插件保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6023622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com