2015-03-19 38 views
0

我有一个Grails控制器操作,它调用将返回包含PDF的base64表示的XML的服务。这是一个(缩写)示例示例。Grails将Base64转换为PDF - 呈现PDF嵌入

<response status="Success"><messages/><values><pages>8</pages></values><xml/><files><pdf name="FitnessApplication" content-type="application/pdf"><![CDATA[JVBERi0xLjQKJeL ... </files></response> 

当我们解析出来后,我想在浏览器中显示该PDF。到目前为止,我能够解码字符串并使用render方法的file属性来渲染它。这可以正确地下载PDF,但我希望它能够在浏览器中显示(内联),而不是作为文件下载。

Map responseMap = new XmlParserHelper().parse(formsResponse.payload) 
byte[] decoded = responseMap.files.pdf.decodeBase64() 
render(file: decoded, fileName: "${document}.pdf", contentType: "application/pdf", ) 

我试着设置内容部署既是一个选项来呈现,并在标题地图,既不似乎做的伎俩。有谁知道我可以如何在浏览器中向用户提供此PDF?

回答

0

只需在回复中发送。但是你需要自己添加标题。例如。是这样的:

response.contentType = "application/pdf" 
response.contentLength = FileUtils.copyFile(pdfFile, response.outputStream) 
+1

感谢您的帮助cfrick,这很好。我在下面发布我的解决方案 – cfaddict 2015-03-19 12:53:58

0

要建立在什么cfrick这里说的是最终的解决方案,我跟去了。

// response data 
byte[] pdfData = responseMap.files.pdf.decodeBase64() 
response.contentType = "application/pdf" 
response.setHeader("Content-disposition", "inline; filename='dan.pdf'") 

// write to output 
OutputStream output = response.outputStream 
output.write(pdfData); 
output.close();