-->

cfpdf merge error when trying to merge multiple pd

2019-08-03 17:53发布

问题:

I'm using CFPDF to merge multiple PDFs together. The PDFs were created using wkhtmltopdf 0.12.1. In most cases the code works fine, but 2 of my documents error whenever merged together. My online searches came up empty and I'm really at a loss. Any help is greatly appreciated.

<cfpdf action="merge" destination="#outputFile#" overwrite="yes">
    <cfloop index="i" from="1" to="#arrayLen(pdfFiles)#">
        <cfpdfparam source="#pdfFiles[i]#">
    </cfloop>
</cfpdf>

The error occurs on line 173

<cfpdfparam source="#pdfFiles[i]#">

cfcatch.message

 com.adobe.internal.pdftoolkit.core.types.ASString cannot be cast to [B 

StackTrace:

java.lang.ClassCastException: com.adobe.internal.pdftoolkit.core.types.ASString cannot be cast to [B
at com.adobe.internal.pdftoolkit.services.interchange.structure.StructureUtil.calculateSuffix(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.impl.PMMNamedDestinations.getNewDestName(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.impl.PMMNamedDestinations.cloneNamedDestination(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.impl.PMMNamedDestinations.cloneDestination(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMBookmark$AppendBookmark.cloneBookmark(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMBookmark$AppendBookmark.firstAction(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMBookmark.walkBookmarksTree(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMBookmark.walkBookmarksTree(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMBookmark.appendBookmarks(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMService.insertPagesinRange(Unknown Source)
at com.adobe.internal.pdftoolkit.services.manipulations.PMMService.appendPages(Unknown Source)
at coldfusion.pdf.PDFDocOperation.merge(PDFDocOperation.java:435)
at coldfusion.tagext.lang.PDFParamTag.doStartTag(PDFParamTag.java:196)
at coldfusion.runtime.CfJspPage._emptyTcfTag(CfJspPage.java:2722)
at cfcombineDocs2ecfm834801932.runPage(D:\inetpub\wwwroot\BUSINESS_APPS\rmsouf\docs\combineDocs.cfm:173)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:231)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:416)
at coldfusion.runtime.CfJspPage._emptyTcfTag(CfJspPage.java:2722)
at cfapplication2ecfc1314889339$funcONREQUEST.runFunction(D:\inetpub\wwwroot\BUSINESS_APPS\rmsouf\application.cfc:155)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:491)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:337)
at coldfusion.runtime.AppEventInvoker.invoke(AppEventInvoker.java:88)
at coldfusion.runtime.AppEventInvoker.onRequest(AppEventInvoker.java:280)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:356)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:94)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70)
at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:79)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
at coldfusion.CfmServlet.service(CfmServlet.java:200)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
at jrun.servlet.FilterChain.service(FilterChain.java:101)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66) 

回答1:

I've run into the same thing with ColdFusion versions 9 and 10, when trying to merge two PDFs (one portrait and one landscape) created with wkhtmltopdf 0.12.2 on Windows Server 2012 R2.

I was able to tinker with the markup generating those PDFs and the merge seemed to bomb whenever my second PDF had any heading tags (h1-6). It seems this has something to do with the "bookmarks" wkhtmltopdf creates out of heading tags.

cfpdf accepts an optional boolean attribute keepBookmark, when merging. Adobe documentation claims this:

Specifies whether bookmarks from the source PDF documents are retained in the merged document

Instead, I find it retains any bookmarks from the first PDF and discards those from the second, but close enough I guess...

I added this parameter to my merge call and it stopped throwing that exception:

var pdfMerger = new PDF();
pdfMerger.addParam(source="xyzzy");
var combinedPDF = pdfMerger.merge(name="combinedPDF", keepBookmark=false);

I haven't tested this in CFML, but it would be

<cfpdf action="merge" destination="#outputFile#" overwrite="yes" keeyBookmark="no">
...
</cfpdf>

A workaround for me, not really a solution.



回答2:

I've encountered the same issue using CF10. (Not tested in CF11 or 2016.) No amount of tweaking ColdFusion/Java settings would enable CF to merge a very simple, small PDF generated using WKHTMLTOPDF even though isPDFFile() = true and CFPDF "Optimize" worked.

I reverted to using CFExecute with PDFtk (GNU General Public License (GPL) Version 2). In addition to working with a wider variety of PDFs, it merges faster, has similar features (compress, watermark, rotate, encrypt) and advanced features (metrics, file attachments, generate FDF Data Stencils, Repair Corrupted PDF, etc).

Here's the command line syntax to merge various PDFs into a single file.

Please note: File paths & names cannot contain spaces. PDFs must be filenames and not stored in RAM as a named variable.

<cfscript>
PDFs = [
    "c:\CFDocument.pdf",
    "c:\WKHTMLTOPDF.pdf",
    "c:\MSWord.pdf",
    "c:\PDFForge.pdf",
    "c:\ActivePDF.pdf"
];
MergedPDF = "c:\PDFtk_merged.pdf";
Args = "#ArrayToList(PDFS, ' ')# cat output #MergedPDF# dont_ask";
</cfscript>

<cfexecute name="c:\PDFtk\bin\pdftk.exe" arguments="#args#" timeOut="60"></cfexecute>


回答3:

I've encountered this issue with adobe pdfs. The problem was the form fields were set to format = numeric but I was passing in strings. Removing the formatting fixed it.