# Swift for Xojo Developers - Part 8a : Graphics : PDF (revisited)

**URL:** <https://ifnotnil.com/t/swift-for-xojo-developers-part-8a-graphics-pdf-revisited/3238>\
**Category:** Swift\
**Tags:** swift\
**Created:** [19 April 2023 21:32 UTC](https://ifnotnil.com/t/swift-for-xojo-developers-part-8a-graphics-pdf-revisited/3238 "2023-04-19T21:32:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [19 April 2023 21:32 UTC](https://ifnotnil.com/t/swift-for-xojo-developers-part-8a-graphics-pdf-revisited/3238/1 "2023-04-19T21:32:02Z")

</div>

In Feb of 2020, I wrote this post

> [@Swift for Xojo Developers - Part 8 : Graphics : PDF](https://ifnotnil.com/t/swift-for-xojo-developers-part-8-graphics-pdf/134):
>
> One of the features that is most talked about for Xojo is the ability to create PDF documents. There are a number of 3rd party tools including DynaPDF and my own gPDF, so I can attest to the complexity of the code involved. Ironically for iOS, this is a relatively simple task. The below code creates the required context and by default is for the US Letter size (but can be easily altered) This class is also subclassed from the previously posted CANVAS class /\* \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*/…

I have just completed a total (well almost total) rewrite of my PDF code, but now it works for both iOS and macOS from a single project file (try THAT Xojo! 🙂 )

This library will support any font currently installed on the device (desktop or iPhone/iPad)  
It supports Unicode characters  
It uses a syntax very similar to those commands found in the Xojo Graphics object (except for Object2D)  
It is written in 100% Swift with zero external dependecies  
The generated PDF document is stored directly to the appropriate /DOCUMENTS folder

here is the basic list of public functions

```auto
# classPDF - a PDF generator for iOS and macOS

Language : Swift5                                   
Platform : iOS and macOS                            
Author : R.David Sisemore                         
Written : 18Apr2023                                
Copyright © 2023 R.DavidSisemore - All rights reserved      

class pdfCanvas {

    public enum lineStyle {
        case none
        case solid
        case dot
        case dash

    }
    public enum pageSizes {
        case letter
        case legal
        case tabloid
        case a4
        case a3
    }

   init(pdfPath:String,pageSize:pageSizes,author:String="",subject:String="",title:String="") {
  
   init(pdfPath:String, pageSize : CGSize = CGSize(width:612,height:792),author:String="",subject:String="",title:String="") {

    public var margin : CGPoint {
 
    //
    // Start a new page
    //
    public func nextPage() {
    //
    // CLOSE PDF Document
    //
    public func close() {
    //
    // Set/Get Font
    //
    public var font : dsFONT {
    *dsFONT* is an alias for either UIFont or NSFont depending on which OS the function is being compiled for
    //
    // Set/Get FontName
    //
    public var textFont : String {
    //
    // Set/Get FontSize
    //
    public var textSize : CGFloat {
    //
    // Set/Get Bold Trait
    //
    public var bold : Bool {
    //
    // Set/Get Italic Trait
    //
    public var italic : Bool {
    //
    // Set/Get Underline Trait
    //
    public var underline : Bool {

    public var textHeight : CGFloat 
    public var textAscent : CGFloat 
    public func stringWidth(text:String) -> CGFloat 
    public func stringHeight(text:String) -> CGFloat 
    //
    // Set/Get ForeColor
    //
    public var foreColor : dsCOLOR {
    *dsCOLOR* is an alias for either UIColor or NSColor depending on which OS the function is being compiled for
    //
    // Set/Get Text Alignment
    //
    public var align : NSTextAlignment {
    //
    // Set the PenSize [ie. Line Drawing Width]
    //
    public var penSize : CGFloat {
    //
    // Set Pen Style [solid, dotted, dashed]
    //
    public var penStyle :lineStyle {
    //
    // Insert an IMAGE into document
    //
    public func drawPicture(img:dsIMAGE,rect:CGRect) {
    //
    // Insert Text into document
    //
    public func drawString(text:String,rect:CGRect) {
    public func drawString(text:String,x:CGFloat,y:CGFloat, width:CGFloat=0) {
    //
    // Draw a line from (X1,Y1) -> (X2,Y2)
    //
    public func drawLine(x1:CGFloat,y1:CGFloat,x2:CGFloat,y2:CGFloat) {
    //
    // OVAL
    //
    public func drawOval(rect:CGRect) { 
    public func drawOval(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 
 
    public func fillOval(rect:CGRect) { 
    public func fillOval(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 
 
    //
    // Rectangle
    //
    public func drawRect(rect:CGRect) { 
    public func drawRect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 
   
    public func fillRect(rect:CGRect) { 
    public func fillRect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 

    //
    // Rounded Rectangle
    //
    public func drawRoundrect(rect:CGRect,radius:CGFloat) {
    public func drawRoundrect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat,radius:CGFloat) { 
   
    public func fillRoundrect(rect:CGRect,radius:CGFloat) { 
    public func fillRoundrect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat,radius:CGFloat) {
    //
    // Polygon
    //
    public func drawPolygon(points:[CGPoint],fill:Bool=false) {
    public func fillPolygon(points:[CGPoint]) { 
}

```

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [20 April 2023 17:56 UTC](https://ifnotnil.com/t/swift-for-xojo-developers-part-8a-graphics-pdf-revisited/3238/2 "2023-04-20T17:56:57Z")

</div>

Updated to allow images to be drawn at any size and maintain aspect ratio,  
Updated to provide automatic word wrapping of long text
