MACSCRPT Archives

March 2010

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Emmanuel LEVY <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Tue, 2 Mar 2010 17:49:56 +0100
Content-Type:
text/plain
Parts/Attachments:
text/plain (144 lines)
I don't know if this can be of any help to anyone, but just in case  
here is how we make pdf documents out of html pages. That's with  
Smile, you know, that famous (free) Applescript editor and a bit more...

-- make a browser window using webkit
set www to make new web window with properties {path name:"http://www.quomodo.com 
"}
-- save the visible screen as jpeg
take screenshot of www in "/Users/emmanuel/Desktop/hardcopy.jpg"
-- save the whole page as pdf
save www in "/Users/emmanuel/Desktop/hardcopy.pdf"

Emmanuel
Satimage-software, the makers of Smile
Quomodo, the extensive users of Smile


On Mar 2, 2010, at 5:42 PM, Steve Alex wrote:

> Wow, it's been a long time since I posted anything on this list. I  
> used to use AppleScript for about everything - even things where it  
> was a bad choice.  I hardly use it anymore - to involved with  
> dynamic web development, but thought I'd share something.
>
> Generating PDFs dynamically from a web application has either been  
> too expensive or did not work very well. I've tried over the years  
> to script Safari to save as pdf, but never perfected it. There are  
> tools out there: wkpdf is a free tool, but does not work well with  
> CSS. PrinceXML is only $3000 per server! We have a plug-in for our  
> database that uses PDFlib, but it is not very easy to use (think  
> Logo, move to, pen down, draw "A", draw box etc). I recently found  
> something that sort of worked  <http://www.ehmac.ca/mac-ipod-help-troubleshooting/48924-applescript-code-request-print-pdf-function-2.html#post650203 
> >, but it didn't quite fit. This is my implementation of that script  
> - probably the first script I've written in years - even using ui  
> scripting which came out about the time I stopped using AppleScript.
>
> Some caveats:
> 	
> 	As written, the usage is a osascript called from the command line.  
> The osascript is passed the folder location of an HTML file and the  
> file name (minus extension). I did this since I needed to call it  
> from a database script. For those interested, the database is the  
> archaic 4D (aka 4th Dimension), but I use a plug-in called Active4D  
> that provides a PHP like scripting architecture. A few changes in  
> the arguments can make it work in different applications
>
> 	For my file based usage, If CSS is used, it must be either url  
> based, relative to the html file location or embedded in the html.
>
> 	My basic database application generates a dynamic html output, but  
> saves it to a file versus sending it to the browser. It then calls  
> Applescript to convert the html file to a  PDF file with the same  
> name. The web script then displays the PDF. Why do this? - no matter  
> how careful you are in creating html, different browsers will render  
> the print differently. This eliminates those differences and just  
> uses Safari.
>
> The script is a little long because of error trapping that may not  
> be required - at least in the depth that I implemented.
>
> #usage : osascript "path to script" "folder path"  "filename"
>
> on run (argv)
> 	set filename to item 2 of argv
> 	set folderPath to item 1 of argv
> 	set htmlFile to filename & ".html"
> 	set htmlPath to folderPath & "/" & htmlFile
> 	set pdfPath to folderPath & "/" & filename & ".pdf"
> 	set err to ""
> 	#do shell script "logger " & err & " Start"
> 	try
> 		do shell script "rm " & pdfPath
> 	end try
> 	
> 	tell application "System Events"
> 		tell application process "Safari"
> 			open location "file://" & htmlPath
> 			set windowRef to a reference to window 1
> 			set noErr to true
> 			click menu item "Print…" of menu "File" of menu bar item "File"  
> of menu bar 1
> 			set noErr to exists sheet 1 of window 1 # print menu should  
> opened sheet 1
> 			if noErr then
> 				click menu button "PDF" of sheet 1 of windowRef
> 			else
> 				set err to "missing menu button PDF"
> 			end if
> 			set noErr to exists menu item "Save as PDF…" of menu 1 of menu  
> button "PDF" of sheet 1 of windowRef
> 			if noErr then
> 				click menu item "Save as PDF…" of menu 1 of menu button "PDF" of  
> sheet 1 of windowRef
> 			else
> 				set err to "missing menu item Save as PDF…"
> 			end if
> 			set noErr to exists window "Save" # show open Save window
> 			if noErr then
> 				keystroke folderPath
> 			else
> 				set err to "Missing window Save"
> 			end if
> 			set noErr to exists button "Go" of sheet 1 of window "Save" #  
> should have opened a folder dialog window seeing a / in the filename
> 			if noErr then
> 				click button "Go" of sheet 1 of window "Save"
> 			else
> 				set err to "Missing button Go of sheet 1 of window Save"
> 			end if
> 			set noErr to exists text field 1 of window "Save" # should be  
> back to save window with Save As text field open
> 			if noErr then
> 				set value of text field 1 of window "Save" to filename & ".pdf"
> 			else
> 				set err to "Missing text field 1 of sheet 1 of window Save"
> 			end if
> 			set noErr to exists button "Save" of window "Save"
> 			if noErr then
> 				click button "Save" of window "Save"
> 				set quittime to (current date) + 30 # allow 30 seconds to save
> 				
> 				repeat while (exists window "Save")
> 					set thisTime to current date
> 					if thisTime > quittime then
> 						beep
> 						set err to "Error saving PDF"
> 						exit repeat
> 					end if
> 				end repeat
> 				click menu item "Close Window" of menu "File" of menu bar item  
> "File" of menu bar 1
> 				if err = "" then set err to "success"
> 			else
> 				set err to "Missing Save button of window Save"
> 			end if
> 			
> 		end tell
> 	end tell
> 	#do shell script "logger " & err & " end"
> 	return err
> end run
>
> Steve Alex

ATOM RSS1 RSS2