-
Notifications
You must be signed in to change notification settings - Fork 3
/
base.cfc
160 lines (144 loc) · 5.67 KB
/
base.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!---
/**
* Copyright (c) 2016, Abram Adams
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
* @version 0.1.0
* @updated 7/22/2016
* @author Abram Adams
******************************************************************************/
--->
<cfcomponent accessors="true">
<cfproperty name="binaryPath" type="string"/>
<cfproperty name="tempDir" type="string"/>
<cfproperty name="_MISSING_URL_HTML_MESSAGE" defaut="You need to provide either a URL or HTML string to convert"/>
<cfscript>
public function create( string url, string html, string destination, struct options = {}, boolean writeToFile = true ){
if( isNull( arguments.url ) && isNull( html ) ){
throw(get_MISSING_URL_HTML_MESSAGE());
}
// setup some wkthmltopdf default options
var defaults = {
"viewport-size":"1200x1080"
,"image-quality":100
,"encoding": "utf-8"
};
structAppend( defaults, options, false );
if( len( trim( html ) ) ){
var tmpFile = "#getTempDir()#_#createUUID()#.html";
fileWrite( tmpFile, html );
arguments.url = tmpFile;
}
// This adds support for inline html injected as header/footer (opposed to passing as url)
var tmpHeaderFile = var tmpFooterFile = "";
if( structKeyExists( arguments.options, "header-html" ) ){
options[ "header-html" ] = _parseHtml( options[ "header-html" ] );
tmpHeaderFile = "#getTempDir()#_#createUUID()#-header.html";
fileWrite( tmpHeaderFile, options[ "header-html" ] );
options[ "header-html" ] = "'#tmpHeaderFile#'";
}
if( structKeyExists( arguments.options, "footer-html" ) ){
options[ "footer-html" ] = _parseHtml( options[ "footer-html" ] );
tmpFooterFile = "#getTempDir()#_#createUUID()#-footer.html";
fileWrite( tmpFooterFile, options[ "footer-html" ] );
options[ "footer-html" ] = "'#tmpFooterFile#'";
}
var args = {
name: getBinaryPath() ,
arguments: _parseOptions( options ) & " '" & arguments.url & "'" ,
destination: isNull( destination ) ? javaCast( "null", "" ) : destination,
timeout: 99999
};
var results = execute( argumentCollection:args );
// Clean up temp files
if( len( trim( html ) ) ){
fileDelete( tmpFile );
}
if( len( trim( tmpHeaderFile ) ) ){
fileDelete( tmpHeaderFile );
}
if( len( trim( tmpFooterFile ) ) ){
fileDelete( tmpFooterFile );
}
if( writeToFile && listLast( getFileFromPath( results.file ), '.' ) == "pdf"){
results.metadata = getInfo( results.file );
}else{
results.metadata = getFileInfo( results.file );
}
results.metadata.html = html;
results.metadata.url = url;
var ret = writeToFile ? results : fileReadBinary( results.file );
if( !writeToFile ){
fileDelete( results.file );
}
return ret;
}
private string function _parseOptions( struct options ){
var parsed = [];
for( var opt in options ){
arrayAppend( parsed, " --#opt##len( options[opt] ) ? ' #options[opt]#' : '' #" );
}
return arrayToList( parsed, ' ' );
}
// html can be inline html, or a url. This will pull down the content and properly
// parse it for wkhtml consumption. Used for header/footer html.
private string function _parseHtml( html ){
if( left( html, 4 ) == "http" ){
var remoteHtml = new Http( url = html ).send().getPrefix();
html = '<!doctype html>' & remoteHtml.fileContent;
if( listFirst( remoteHtml.mimeType, '/') == "image" ){
return '<!doctype html><img src="data:#remoteHtml.mimeType#;base64,#toBase64( remoteHtml.fileContent )#"/>';
}
}
return html;
}
</cfscript>
<cffunction name="execute" access="public">
<cfargument name="destination" default="#getTempDirectory()#wkhtmltopdf-#hash(createUUID())#.pdf"/>
<cfset arguments.timeout = 120/>
<cfset var originalOutputFile = arguments.destination />
<cfset arguments.arguments &= " '" & arguments.destination & "'"/>
<cfset var results = {}/>
<cfset local.stdout = "" />
<cfset local.stderr = "" />
<cfset arguments.variable = "local.stdout" />
<cfset arguments.errorVariable = "local.stderr" />
<cftry>
<cfset results.executionTime = getTickCount()/>
<cfexecute attributeCollection="#arguments#"></cfexecute>
<cfset results.executionTime = getTickCount() - results.executionTime/>
<cfset results.command = name />
<cfset results.options = arguments.arguments />
<cfset results.output = trim( local.stdout ) />
<cfset results.error = !isNull( local.stderr ) && len( trim( local.stderr ) )
? trim( local.stderr )
: findNoCase( "permission denied", local.stdout )
? trim( local.stdout ) : ""/>
<cfset results.file = destination />
<cfif len( trim( results.error ) )>
<cfthrow message="#results.error#">
</cfif>
<cfcatch type="any">
<!--- <cfdump var="#[arguments,cfcatch]#" abort> --->
<!--- <cfthrow message="Error converting to PDF" detail="#cfcatch.detail#"> --->
</cfcatch>
</cftry>
<cfif !isNull( originalOutputFile ) && destination != originalOutputFile>
<cffile action="move" source="#destination#" destination="#originalOutputFile#">
<cfset results.file = originalOutputFile />
</cfif>
<cfreturn results />
</cffunction>
</cfcomponent>