我只是想知道是否还有其他选项可以使用<cfdocument>
可以利用网络套件API进行ColdFusion。
或者,如果有一个可用的Java,但它上面有一个很好的ColdFusion包装器,比使用java的所有复杂功能更容易。
答案 0 :(得分:3)
我深入了解并使用iText。这样做我创建了一个非常强大的打印框架(用于打印表单)和报告框架(表格报告)。多年来我对它进行了很好的调整,它可以生成数十万行的PDF没问题。它可以深入4个孩子,并具有许多功能/设置。对不起这么广泛。如果你走这条路,我可以提供具体的帮助。
我在onApplicationStart中使用JavaLoader(http://javaloader.riaforge.org/)来加载iText(以及我在我的应用中需要的其他jar)......
<!--- note, this is actually a harcoded UUID value --->
<cfset MyUniqueKeyForJavaLoader = "1111-2222-3333444455556666">
<!--- if we have not already created the javaLoader --->
<cfif not structKeyExists(server, MyUniqueKeyForJavaLoader)>
<!--- construct an array containing the full path to the jars you wish to load --->
<cfset pathArray = arrayNew(1)>
<cfset arrayAppend(pathArray, expandpath('jars/iText-2.1.3.jar'))>
<!---<cfset arrayAppend(pathArray, expandpath('jars/iText-5.0.6.jar'))>--->
<cfset arrayAppend(pathArray, expandpath('jars/IDADataMatrix.jar'))>
<cfset arrayAppend(pathArray, expandpath('jars/LinearBarCode.jar'))>
<cfset arrayAppend(pathArray, expandpath('jars/barcodeencoder.jar'))>
<cfset arrayAppend(pathArray, expandpath('jars/sshcommandexecutor.jar'))>
<!--- <cfset arrayAppend(pathArray, expandpath('jars/jsch-0.1.40.jar'))> --->
<cflock scope="server" type="exclusive" timeout="10">
<!--- verify again the javaloader was not already created --->
<cfif not StructKeyExists(server, MyUniqueKeyForJavaLoader)>
<cfset server[MyUniqueKeyForJavaLoader] = createObject("component", "javaloader.JavaLoader").init(pathArray)>
</cfif>
</cflock>
</cfif>
然后,在报告框架中,您将开始初始化所有内容。 (这只是一个小片段,除此之外还有很多东西,但是如果你决定,它应该足以让你开始愚弄。)
<cfscript>
pathAndFile = request.reportDir.fpath&request.reportDir.pdfFile;
session.reports[libRptId].pathAndFile = pathAndFile;
loader = server['1111-2222-3333444455556666']; //Our iText version which currently is 2.1.3 (yes, we need to update soon)
document = loader.create("com.lowagie.text.Document");
PdfWriter = loader.create("com.lowagie.text.pdf.PdfWriter");
FileOutputStream = createObject("java", "java.io.FileOutputStream");
myFile = CreateObject("java","java.io.File").init(pathAndFile); //Only used for getting the file size to display on screen in real-time as the pdf is being generated
Color = createObject("java", "java.awt.Color");
element = loader.create("com.lowagie.text.Element");
Chunk = loader.create("com.lowagie.text.Chunk");
PageSize = loader.create("com.lowagie.text.PageSize");
HeaderFooter = loader.create("com.lowagie.text.HeaderFooter");
Rectangle = loader.create("com.lowagie.text.Rectangle");
Paragraph = loader.create("com.lowagie.text.Paragraph");
PdfPCell = loader.create("com.lowagie.text.pdf.PdfPCell");
PdfPTable = loader.create("com.lowagie.text.pdf.PdfPTable");
Phrase = loader.create("com.lowagie.text.Phrase");
Font = loader.create("com.lowagie.text.Font");
FontFactory = loader.create("com.lowagie.text.FontFactory");
</cfscript>
<!--- Define our different fonts --->
<cfset fontTitle = FontFactory.getFont("Arial", 10, Font.BOLD, Color.BLACK)>
<cfset fontSubRptTitle = FontFactory.getFont("Arial", 10, Font.BOLDITALIC, Color.BLACK)>
<cfset fontStandard = FontFactory.getFont("Arial", 8, Font.NORMAL, Color.BLACK)>
<cfset fontHeader = FontFactory.getFont("Arial", 9, Font.NORMAL, Color.GRAY)>
<cfset fontFooter = FontFactory.getFont("Arial", 9, Font.NORMAL, Color.GRAY)>
<cfset fontColHeader = FontFactory.getFont("Arial", rpt.fontsizeLabel, Font.BOLD, Color.BLACK)>
<cfset fontData = FontFactory.getFont("Arial", rpt.fontsize, Font.NORMAL, Color.BLACK)>
<cfset marginTop = round(72 / (100 / (rpt.marginTop * 100))) >
<cfset marginright = round(72 / (100 / (rpt.marginright * 100))) >
<cfset marginbottom = round(72 / (100 / (rpt.marginbottom * 100))) >
<cfset marginleft = round(72 / (100 / (rpt.marginleft * 100))) >
<!--- Page Setup (PageSize and Margins) For the margins 18 = .25inches --->
<cfif rpt.pagetype eq "letter">
<cfif rpt.orientation eq "portrait">
<cfset document = document.init(PageSize.LETTER, marginleft, marginright, marginTop, marginbottom)>
<cfelse><!--- Landscape --->
<cfset document = document.init(PageSize.LETTER.rotate(), marginleft, marginright, marginTop, marginbottom)>
</cfif>
<cfelseif rpt.pagetype eq "legal">
<cfif rpt.orientation eq "portrait">
<cfset document = document.init(PageSize.LEGAL, marginleft, marginright, marginTop, marginbottom)>
<cfelse><!--- Landscape --->
<cfset document = document.init(PageSize.LEGAL.rotate(), marginleft, marginright, marginTop, marginbottom)>
</cfif>
<cfelseif rpt.pagetype eq "ledger">
<!--- We found that ledger logic is backward from letter and legal. Long edge is portrait 11 X 17 and landscape mode = 17 X 11 --->
<cfif rpt.orientation eq "portrait">
<cfset document = document.init(PageSize.LEDGER.rotate(), marginleft, marginright, marginTop, marginbottom)>
<cfelse><!--- Landscape --->
<cfset document = document.init(PageSize.LEDGER, marginleft, marginright, marginTop, marginbottom)>
</cfif>
</cfif>
<cfset writer = PdfWriter.getInstance(document, FileOutputStream.init(pathAndFile))> <!--- Init the variable "document" that we write to --->
答案 1 :(得分:0)
我们使用一个名为wkhtmltopdf的小应用程序,它允许您从任何HTML页面创建PDF。它适用于命令行,因此您可以使用<cfexecute>
来运行它。
我还使用以下代码确保PDF在输出之前完全形成:
<cfset truePDF = "false">
<cfloop condition="NOT truePDF">
<cfif fileExists("#pathname##filename#")>
<cffile action="read" file="#pathname##filename#" variable="pdfContent">
<cfif findNoCase('%%EOF',right(pdfContent,1024)) GT 0>
<cfset truePDF = "true">
</cfif>
</cfif>
</cfloop>
<cfoutput>#pdfContent#</cfoutput>