cfhtmltopdf with svg images

Community Beginner ,
Dec 06, 2020 Dec 06, 2020

Copy link to clipboard

Copied

Has anyone gotten <cfhtmltopdf> to work with svg (or any other vector image formats). I have a require ment for very large vector pdfs that can be imported into CAD. I have tried using wkhtmltopdf but have had issues with it using local files on the server (on a stand alone work station it works great) and finding a way for it to push the completed pdf back to the user rather than doing a local save on the server. Thus if I could get <cfhtmltopdf> to work with the vector images it would be a godsend.

Views

201

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 07, 2020 Dec 07, 2020

Copy link to clipboard

Copied

I prefer using SVG files with WKHTMLTOPDF as it results in a smaller PDF with higher quality results. Using a webserver is critical so that WK can determine the correct MIME TYPE.  (I also use FontAwesome with WK... but web server-based mime types are important.)

Generating a local PDF file on the server shouldn't be a real issue.  (I'm sure that CFHTMLTOPDF does the same thing since it supposedly also uses an older version of WKHTMLTOPDF behind the scenes.)  We prefer generating the file and saving it to the server for a number of reasons (troubleshooting, quality review, performance, etc).  We run a scheduled clean up job afterwards to remove any temporary files (BAT file, temporarily resized images, etc). After creating the PDF file, there should be anything prohibiting you from sending a download header and using CFContent with file/delete attributes.  This should provide the same user experience as CFHTMLTOPDF "pushing the completed PDF back to the user".

<cfheader name="content-disposition" value="attachment; filename="c:\myPDF.pdf">
<cfcontent type="application/unknown" file="c:\myPDF.pdf" deletefile="Yes">


Here's another community post that shows comparisons between to two engines.
https://community.adobe.com/t5/coldfusion/why-does-cfhtmltopdf-suck/m-p/11516137?page=1#M186760

My personal advice since using WKHTMLTOPDF with ColdFusion 9 and reviewing CFHTMLTOPDF is to continue using WKHTMLTOPDF.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2020 Dec 07, 2020

Copy link to clipboard

Copied

Jamo, thx for the response.

   Please bear with me on this, it's a whole new ballgame for me. I have a rather complex cfm page that allows the user to build a custom assembly (a street light) from a variety of components rendered as html. Once the user has completed an assembly to their liking I need to be able to generate a vector pdf of that assembly from svg source files. I've got all the html/cfml working, now I have to find a way to output it. I've tried to incorporate WKHTMLTOPDF into the page but couldn't figure out a way to parse the assembly directly into WKHTMLTOPDF. I also tried to use an intermediate cfm page that incorporates WKHTMLTOPDF by calling out the actaul cfm assembly page as an argument, I had mixed luck with this. It worked right up to the point where I tried to call out a cfm (or html) page on the same server then failed to execute (as stated in my prior post it works fine on a workstation calling out local files with '--enable-local-file-access' enabled). I've been trying to run WKHTMLTOPDF with <cfexecute>.

   I get the gist from your response, but it's moot until I can render the PDF.

 

thx

Ry

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 07, 2020 Dec 07, 2020

Copy link to clipboard

Copied

Our approach has been to save the static html document to the web path using a temporary filename prior to PDF generation.  This was a better approach than having WKHTMLTOPDF access the content via our web application.  We didn't want it to be negatively impacted by any app security rules.  In some cases, we also generate a dedivated BAT file (with same temp filename) that we can call using CFExecute.  This enables us to manually run the BAT file ourselves on the server to troubleshoot and view any errors that may be reported only in the console.  (We've encountered some odd issues that caused PDF generation to abort, but nothing that we haven't been able to work around yet.)

 

QUESTION:  Forget WK for the moment, are you able to render a standalone HTML page that is printable via Chrome?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 09, 2020 Dec 09, 2020

Copy link to clipboard

Copied

Jamo

Thank you. With your tips I got it all to work. I also used the <cfile> to delete the html after the PDF build and set the <cfheader> & <cfcontent> as you listed above. So the user is offered the download & all the generated files are gone in a flash for a clean exit.

  As for the print from Chrome; As they say "Houston we have a problem", the generated html/PDF pages are so large (400in wide x 600in high) there is really no way to print them.

 

Ry

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

Thanks, Ry and Jamo. I have reported a bug: https://tracker.adobe.com/#/view/CF-4210977

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

On a related topic, if you wanted to convert a Scalable Vector Graphics (SVG) image to PDF, you could proceed as follows.

1) Create a directory, SvgToPdf, under ColdFusion's web root. Create the subdirectory, lib, in SvgToPdf.
2) Download the FOP library, 

     fop-2.5-bin.zip

    from https://xmlgraphics.apache.org/fop/download.html and unzip it.

3) Copy the following JAR files to the directory /SvgToPdf/lib/

     fop-2.5-bin\fop-2.5\fop\build\fop.jar
     fop-2.5-bin\fop-2.5\fop\lib\batik-all-1.13.jar
     fop-2.5-bin\fop-2.5\fop\lib\commons-io-1.3.1.jar
     fop-2.5-bin\fop-2.5\fop\lib\xmlgraphics-commons-2.4.jar

That is, on your system, their paths should be, respectively,

    /SvgToPdf/lib/fop.jar

    /SvgToPdf/lib/batik-all-1.13.jar

    /SvgToPdf/lib/commons-io-1.3.1.jar

    /SvgToPdf/lib/xmlgraphics-commons-2.4.jar

They are the libraries to be used for the SVG to PDF conversion. We need to inject them into ColdFusion.

We shall use the javasettings property to do so. Therefore, create within the directory SvgToPdf an Application.cfc file having the following content:

 

<cfcomponent displayname="Application" output="true" hint="Handle the application.">
	<!--- Set up the application. --->
	<cfset this.Name = "SVG2PDF" />
	<cfset this.ApplicationTimeout = createTimeSpan( 1, 0, 0, 0 ) />
	<cfset this.sessionTimeout = createTimeSpan( 0, 0, 30, 0 ) />
	<cfset this.sessionManagement = true />
	<cfset this.javaSettings = {LoadPaths = [ "#expandPath(".")#\lib"]}>

	<cffunction
		name="OnApplicationStart"
		access="public"
		returntype="boolean"
		output="false"
		hint="Fires when the application is first created.">
		
		<cfreturn true />
	</cffunction>	
</cfcomponent>

 

4) Within the directory SvgToPdf, create a CFM page, generatePdfFromSvg.cfm. It contains the following code:

 

<cfscript>
// The input and output
inputFilePath = expandPath("1607519675floral_pattern_geometric.svg");
outputFilePath = expandPath("outputDoc.pdf");

// Transcode the SVG input
inputFile = createobject("java", "java.io.File").init(inputFilePath);
fileInputStream = createobject("java", "java.io.FileInputStream").init(inputFile);
transcoderInput = createobject("java", "org.apache.batik.transcoder.TranscoderInput").init(fileInputStream);

// Create the output stream
outputFile = createobject("java", "java.io.File").init(outputFilePath);
fileOutputStream = createobject("java", "java.io.FileOutputStream").init(outputFile);

// Transcode the input into the output
PDFTranscoder = createobject("java", "org.apache.fop.svg.PDFTranscoder").init();
transcoderOutput = createobject("java", "org.apache.batik.transcoder.TranscoderOutput").init(fileOutputStream);
PDFTranscoder.transcode(transcoderInput, transcoderOutput);

// Close the streams to release fle-system resources
fileInputStream.close();
fileOutputStream.close();
</cfscript>

<cfheader name="Content-Disposition" value="inline;filename=#outputFilePath#"> 
<cfcontent type="application/pdf" file="#outputFilePath#" deleteFile="no" />

 

5) Launch the file generatePdfFromSvg.cfm

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2020 Dec 14, 2020

Copy link to clipboard

Copied

BKBK thx for the input.

   I've been going through the FOP documentation and have a question. The final assemby that needs to be converted is a collection of individual precisely located SVG images (anywhere from one to over a dozen separate images). Hence, this is where the html comes in, to control individual image locations. Can the <cfsavecontent> & <cfxml> tags some how be used in lieu of an input image file?

Ry

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 14, 2020 Dec 14, 2020

Copy link to clipboard

Copied

I don't quite understand. Do you mean the SVG comes in in the form of XML embedded within cfsavecontent?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2020 Dec 14, 2020

Copy link to clipboard

Copied

At this time the <cfsavecontent variable="cachedOutput"> saves the cfml output (i don't know what form that content is stored as) and then

<cffile action="write" file="cachedFile.html" output="#cachedOutput#">

creates a normal html file with <img src ..> tags pointing to the SVG files. WKHTMLTOPDF is used to convert this html file (with images) to PDF.

Can FOP render an html file, sorry I should have asked that first?

Ry

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 15, 2020 Dec 15, 2020

Copy link to clipboard

Copied

LATEST

Ah, thanks. I understand now.

Given the use-case you describe, I wouldn't bother with FOP. I think WKHTMLTOPDF is the best choice.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines