-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkhtmltopdf_pdf_generator.cfc
68 lines (55 loc) · 2.48 KB
/
wkhtmltopdf_pdf_generator.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<cfcomponent extends="base_pdf_generator">
<!--- generate a PDF and push it to the output stream --->
<cffunction name="generate" returntype="void" output="true" access="public">
<cfset var outFile = generateFile()>
<cfcontent type="application/pdf" reset="true" file="#outFile#">
</cffunction>
<!--- generate a PDF in a tmp file and return the full path to the file --->
<cffunction name="generateFile" returntype="string" output="false" access="public">
<cfset var execArgs = baseExecArgs()>
<cfif StructKeyExists(variables, 'headerHTML')>
<cfset var headerFile = uniqueTempFile()>
<cfset FileWrite(headerFile, getHeader())>
<cfset ArrayAppend(execArgs, '--header-html #headerFile#')>
</cfif>
<cfif StructKeyExists(variables, 'footerHTML')>
<cfset var footerFile = uniqueTempFile()>
<cfset FileWrite(footerFile, getFooter())>
<cfset ArrayAppend(execArgs, '--footer-html #footerFile#')>
</cfif>
<cfset var bodyFile = uniqueTempFile()>
<cfset FileWrite(bodyFile, ArrayToList(variables.body_parts, ' '))>
<cfset ArrayAppend(execArgs, bodyFile)>
<cfset var outFile = uniqueTempFile('pdf')>
<cfset ArrayAppend(execArgs, outFile)>
<cfexecute name="/usr/local/bin/wkhtmltopdf" arguments="#ArrayToList(execArgs, ' ')#" timeout="#variables.executeTimeout#"></cfexecute>
<cfreturn outFile>
</cffunction>
<cfscript>
private array function baseExecArgs(){
var args = [
'--page-size #variables.pagetype#'
, '--orientation #variables.orientation#'
, '--encoding utf-8'
, '--enable-local-file-access'
];
for( var key in variables.margins ){
ArrayAppend(args, '--margin-#key# #variables.margins[key]#');
}
for( var key in variables.addlOptions ){
ArrayAppend(args, '--#key# #variables.addlOptions[key]#');
}
return args;
}
private struct function documentVariableMapping(){
/* The javascript here is an adaptation of some code in "Footers And Headers"
https://github.com/antialize/wkhtmltopdf/blob/master/README_WKHTMLTOPDF#L312-L332
The replacement values are in the query string for the document */
return { '$currentPageNumber$' = '<script>document.write(("&" + document.location.search.substring(1) ).split("&page=")[1].split("&")[0]);</script>'
, '$totalPageCount$' = '<script>document.write(("&" + document.location.search.substring(1) ).split("topage=")[1].split("&")[0]);</script>'};
}
private string function uniqueTempFile(string ext = 'html'){
return GetTempDirectory() & 'pdfGen' & Replace(CreateUUID(), '-', '', 'all') & '.' & arguments.ext;
}
</cfscript>
</cfcomponent>