• 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
Convert HTML to PDF
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Convert HTML to PDF


  • Subject: Convert HTML to PDF
  • From: Takaaki Naganoya <email@hidden>
  • Date: Wed, 13 Sep 2017 13:56:31 +0900

This script converts html to PDF without pagenation.
Is there a way to output paginated PDF?

Takaaki Naganoya

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

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

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 aStyledText to htmlToStyledText(aFile) of me
set bRes to saveStyledTextAsPDF(path2, aStyledText) of me


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(thePath, aStyledString)
        -- get page size being used for printing
        set printInfo to NSPrintInfo's sharedPrintInfo()
        set pageSize to printInfo's paperSize()
        set theLeft to printInfo's leftMargin()
        set theTop to printInfo's topMargin()

        -- make a text view
        set theView to NSTextView's alloc()'s initWithFrame:{origin:{x:0, y:0},
|size|:pageSize}
        theView's setTextContainerInset:{theLeft, theTop}

        -- put in the text
        theView's textStorage()'s setAttributedString:aStyledString
        set theData to theView's dataWithPDFInsideRect:{origin:{x:0, y:0},
|size|:pageSize}

        return (theData's writeToFile:thePath atomically:true) as boolean
end saveStyledTextAsPDF
</AppleScript>

--
Takaaki Naganoya
email@hidden
http://piyocast.com/as/



 _______________________________________________
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: Shane Stanley <email@hidden>
  • Prev by Date: Re: [applescript] Slow code
  • Next by Date: Re: Convert HTML to PDF
  • Previous by thread: Re: Saving an encrypted PDF in Word 2016
  • Next by thread: Re: Convert HTML to PDF
  • Index(es):
    • Date
    • Thread