如何将ColdFusion页面转换为PDF下载? [英] How do I turn a ColdFusion page into a PDF download?

查看:106
本文介绍了如何将ColdFusion页面转换为PDF下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将CFM页面生成的HTML转换为PDF,并在导航到我的页面时让用户看到标准的另存为"提示.

I would like to turn the HTML generated by my CFM page into a PDF, and have the user prompted with the standard "Save As" prompt when navigating to my page.

推荐答案

您应使用cfdocument标记(格式为"PDF")将PDF放置在要生成的页面周围,以生成PDF.您需要指定文件名属性,否则文档将直接流到您的浏览器.

You should use the cfdocument tag (with format="PDF") to generate the PDF by placing it around the page you are generating. You'll want to specify a filename attribute, otherwise the document will just stream right to your browser.

将内容另存为PDF后,结合使用cfheader和cfcontent将PDF作为附件输出(另存为")并将文件添加到响应流中.我还在cfcontent标记上添加了deletefile ="Yes",以保持文件系统中文件的清洁.

After you have saved the content as a PDF, use cfheader and cfcontent in combination to output the PDF as an attachment ("Save As") and add the file to the response stream. I also added deletefile="Yes" on the cfcontent tag to keep the file system clean of the files.

<cfdocument format="PDF" filename="file.pdf" overwrite="Yes">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    Hello World
</body>
</html>
</cfdocument>
<cfheader name="Content-Disposition" value="attachment;filename=file.pdf">
<cfcontent type="application/octet-stream" file="#expandPath('.')#\file.pdf" deletefile="Yes">

顺便说一句:在下面的示例中,我仅使用file.pdf作为文件名,但是您可能希望使用一些随机或会话生成的字符串作为文件名,以避免由于竞争条件而产生问题.

As an aside: I'm just using file.pdf for the filename in the example below, but you might want to use some random or session generated string for the filename to avoid problems resulting from race conditions.

这篇关于如何将ColdFusion页面转换为PDF下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆