Monday, November 20, 2017

#1 2017-10-27 03:43:29 am

From:: South Tirol
Registered: 2008-07-04
Posts: 521

Html to Pdf

I downloaded and installed  wkhtmltopdf
.. but got just some files installed in a hidden directory, nothing more.
The homepage doesn't explain anything. big_smile

What I want is a shell command or at least a wrapper shell to execute the downloaded code from Applescript. Some help?



#2 2017-10-27 06:15:05 pm

Shane Stanley
From:: Australia
Registered: 2002-12-07
Posts: 5177

Re: Html to Pdf

Here's an alternative you could try:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

-- classes, constants, and enums used
property NSAutoPagination : a reference to 0
property NSClipPagination : a reference to 2
property NSThread : a reference to current application's NSThread
property NSPrintJobSavingURL : a reference to current application's NSPrintJobSavingURL
property NSPrintOperation : a reference to current application's NSPrintOperation
property NSPrintSaveJob : a reference to current application's NSPrintSaveJob
property |NSURL| : a reference to current application's |NSURL|
property NSString : a reference to current application's NSString
property NSTextView : a reference to current application's NSTextView
property NSPrintInfo : a reference to current application's NSPrintInfo
property NSAttributedString : a reference to current application's NSAttributedString
property NSData : a reference to current application's NSData
property NSUUID : a reference to current application's NSUUID
property NSDictionary : a reference to current application's NSDictionary

property theResult : false -- whether it succeeded or not

set thePath to POSIX path of (choose file of type {"public.html"})

set thePath to NSString's stringWithString:thePath
set newPath to thePath's stringByDeletingPathExtension()'s stringByAppendingPathExtension:"pdf"

set styledText to my styledTextFromHTMLFile:thePath
my saveStyledText:styledText asPDFToFile:newPath

on styledTextFromHTMLFile:thePath
   set theData to NSData's dataWithContentsOfFile:thePath
   set attStr to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value)
   return attStr
end styledTextFromHTMLFile:

on saveStyledText:styledText asPDFToFile:newPath
   -- create print info for saving to file
   set destURL to |NSURL|'s fileURLWithPath:newPath
   set printInfo to NSPrintInfo's alloc()'s initWithDictionary:(NSDictionary's dictionaryWithObject:destURL forKey:(NSPrintJobSavingURL)) -- sets destination
   printInfo's setJobDisposition:NSPrintSaveJob -- save to file job
   printInfo's setHorizontalPagination:NSClipPagination
   printInfo's setVerticalPagination:NSAutoPagination
   -- get page size and margins
   set pageSize to printInfo's paperSize()
   set theLeft to printInfo's leftMargin()
   set theRight to printInfo's rightMargin()
   set theTop to printInfo's topMargin()
   -- make a very deep text view
   set theView to NSTextView's alloc()'s initWithFrame:{{0, 0}, {(pageSize's width) - theLeft - theRight, 3.0E+38}}
   theView's setHorizontallyResizable:false
   -- put in the text
   theView's textStorage()'s setAttributedString:styledText
   -- size to fit; must be done on the main thread
   if NSThread's isMainThread() then
       theView's sizeToFit()
       theView's performSelectorOnMainThread:"sizeToFit" withObject:(missing value) waitUntilDone:true
   end if
   -- create print operation and run it
   set printOp to NSPrintOperation's printOperationWithView:theView printInfo:printInfo
   printOp's setShowsPrintPanel:false
   printOp's setShowsProgressPanel:false
   if NSThread's isMainThread() then
       set my theResult to printOp's runOperation()
       my performSelectorOnMainThread:"runPrintOperation:" withObject:printOp waitUntilDone:true
   end if
end saveStyledText:asPDFToFile:

on runPrintOperation:printOp -- on main thread
   set my theResult to printOp's runOperation()
end runPrintOperation:

Shane Stanley <>



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)