2014-01-15 36 views
1

我只是想知道是否有其他選項來使用<cfdocument>,可以利用ColdFusion的web-kit API。ColdFusion PDF生成 - 其他選項cfdocument

或者,如果有一個在Java中可用,但它有一個很好的ColdFusion包裝,使它比解決Java的所有錯綜複雜問題更容易。

+0

您正在使用什麼版本的ColdFusion? –

+0

高到有一個cfpdf標籤? –

回答

3

我在引擎蓋下使用iText。爲此,我創建了一個非常強大的打印框架(用於打印表單)和報表框架(表格報表)。我很好的調整了多年,它可以生成數十萬行的PDF沒有問題。它可以深入4個孩子,並具有許多功能/設置。對不起這麼寬泛。如果你走這條路,我可以提供具體的幫助。

我使用JavaLoader(http://javaloader.riaforge.org/)在我onApplicationStart加載的iText(和其他罐子我需要在我的應用程序).​​..

<!--- 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 ---> 
+0

@ user125264,我添加了更多的細節來回答。 – gfrobenius

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>