如何将ColdFusion页面转换为PDF下载?

Luc*_*ers 14 pdf coldfusion

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

Sol*_*nal 17

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

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

<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">
Run Code Online (Sandbox Code Playgroud)

暂且不说:我只是在下面的示例中使用file.pdf作为文件名,但您可能希望使用一些随机或会话生成的字符串作为文件名,以避免因竞争条件导致的问题.