Last active
December 28, 2015 10:59
-
-
Save iamjono/7489893 to your computer and use it in GitHub Desktop.
wkhtmltopdf Lasso 9 type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
/* ======================================================================================== | |
wkhtmltopdf | |
https://code.google.com/p/wkhtmltopdf/ | |
wkhtmltopdf('http://www.example.com/mypage.lasso?id=83','/pdfcache/id83.pdf',-removelast) | |
Requires std_shellhandler trait | |
======================================================================================== */ | |
define wkhtmltopdf => type { | |
trait { | |
import std_shellhandler | |
} | |
data | |
private binary = 'wkhtmltopdf', | |
private defaultoptions::array = array('page-size' = 'Letter','quiet','disable-javascript'), | |
public options::array = array | |
public oncreate(source::string,file::string,-options::array=array,-removelast::boolean=false) => { | |
#options->size ? .options = #options | |
local(cachepath = file_ForceRoot(#file)->split('/')) | |
local(actualfile = #cachepath->last) | |
#cachepath->removeLast | |
local(cachepathclean = #cachepath->join('/')) | |
#cachepathclean->replace('//','/') | |
if(#removelast) => { | |
local(str = .binary + .assembleOptions + #source + ' ' + #cachepathclean+'/temp.pdf') | |
.shellhandler(#str) | |
local(str = 'gs -dNOPAUSE -dBATCH -dFirstPage=1 -dLastPage=1 -sDEVICE=pdfwrite -sOutputFile='+#cachepathclean+'/'+#actualfile+' -f '+#cachepathclean+'/temp.pdf') | |
.shellhandler(#str) | |
else | |
.shellhandler(.binary + .assembleOptions + #source + ' ' + #cachepathclean+'/'+#actualfile) | |
} | |
} | |
private assembleOptions() => { | |
/* ============================================================================= | |
options best to get from | |
http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html | |
this private method accepts either pairs or singletons | |
============================================================================= */ | |
// merge default otions into th eoptions array | |
with d in .defaultoptions do => { | |
if(#d->isA('pair')) => { | |
not(.options >> #d->first) ? .options->insert(#d) | |
else | |
not(.options >> #d) ? .options->insert(#d) | |
} | |
} | |
local(x = array) | |
with o in .options do => { | |
if(#o->isA('pair')) => { | |
#o->first->beginswith('-') ? #x->insert(#o->first+' '+#o->value) | #x->insert('--'+#o->first+' '+#o->value) | |
else | |
#o->beginswith('-') ? #x->insert(#o) | #x->insert('--'+#o) | |
} | |
} | |
return ' ' + #x->join(' ') + ' ' | |
} | |
} // end wkhtmltopdf type | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment