• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Convert HTML to PDF
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convert HTML to PDF


  • Subject: Re: Convert HTML to PDF
  • From: Shane Stanley <email@hidden>
  • Date: Wed, 13 Sep 2017 17:43:35 +1000

On 13 Sep 2017, at 2:56 pm, Takaaki Naganoya <email@hidden> wrote:
>
> Is there a way to output paginated PDF?

Yes, although it's a little more complex -- you have to set up and run a print
operation.

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 bRes : false

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

set thePath to NSString's stringWithString:aFile
set path1 to thePath's stringByDeletingLastPathComponent()
set theName to NSUUID's UUID()'s UUIDString()
set path2 to (path1's stringByAppendingPathComponent:theName)'s
stringByAppendingPathExtension:"pdf"
set theURL to |NSURL|'s fileURLWithPath:path2

set aStyledText to my htmlToStyledText(aFile)
my saveStyledTextAsPDF(theURL, aStyledText)

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

on saveStyledTextAsPDF(theURL, aStyledString)
        -- create print info for saving to file
        set printInfo to NSPrintInfo's alloc()'s
initWithDictionary:(NSDictionary's dictionaryWithObject:theURL
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 theTop to printInfo's topMargin()
        -- make a very deep text view
        set theView to NSTextView's alloc()'s initWithFrame:{origin:{x:0, y:0},
|size|:{width:pageSize's width, height:3.0E+38}}
        theView's setTextContainerInset:{theLeft, theTop}
        theView's setHorizontallyResizable:false
        -- put in the text
        theView's textStorage()'s setAttributedString:aStyledString
        -- size to fit; must be done on the main thread
        if NSThread's isMainThread() then
                theView's sizeToFit()
        else
                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 bRes to printOp's runOperation()
        else
                my performSelectorOnMainThread:"runPrintOperation:"
withObject:printOp waitUntilDone:true
        end if
end saveStyledTextAsPDF

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

--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Convert HTML to PDF
      • From: Takaaki Naganoya <email@hidden>
References: 
 >Convert HTML to PDF (From: Takaaki Naganoya <email@hidden>)

  • Prev by Date: Convert HTML to PDF
  • Next by Date: Re: Convert HTML to PDF
  • Previous by thread: Convert HTML to PDF
  • Next by thread: Re: Convert HTML to PDF
  • Index(es):
    • Date
    • Thread