SimplePDF is a wrapper of UIGraphics PDF context written in Swift.

Last update: Jun 7, 2022

Simple PDF

Platform: iOS 8.0+ Language: Swift 4 Cocoapods

SimplePDF is a wrapper of UIGraphics PDF context written in Swift.

You can:

  • add texts, images, spaces and lines, table
  • set up page layout, adjust content alignment
  • generate PDF data/file.

In summary, you can create a simple PDF effortlessly. 😄

Example

let A4paperSize = CGSize(width: 595, height: 842)
let pdf = SimplePDF(pageSize: A4paperSize)

pdf.addText("Hello World!")
// or
// pdf.addText("Hello World!", font: myFont, textColor: myTextColor)

pdf.addImage( anImage )

let dataArray = [["Test1", "Test2"],["Test3", "Test4"]]
pdf.addTable(rowCount: 2, columnCount: 2, rowHeight: 20.0, columnWidth: 30.0, tableLineWidth: 1.0, font: UIFont.systemFontOfSize(5.0), dataArray: dataArray)

let pdfData = pdf.generatePDFdata()

// save as a local file
try? pdfData.writeToFile(path, options: .DataWritingAtomic)

See the result example.pdf, which is generated from ExampleCode.swift.

Installation

via Cocoapods

use_frameworks!
pod 'SimplePDF'

Usage

import SimplePDF

Page Setup

page setup

let A4paperSize = CGSize(width: 595, height: 842)
let pdf = SimplePDF(pageSize: A4paperSize, pageMargin: 20.0)
// or define all margins extra
let pdf = SimplePDF(pageSize: A4paperSize, pageMarginLeft: 35, pageMarginTop: 50, pageMarginBottom: 40, pageMarginRight: 35)

Write Something

pdf.addText( "some text" )
pdf.addImage( UIImage )
pdf.addAttributedText( NSAttributedString )
pdf.addLineSeparator(height: 30) // or pdf.addLineSeparator() default height is 1.0
pdf.addLineSpace(20)

Layout

You can layout horizontally and vertically

// Start a horizonal arrangement
pdf.beginHorizontalArrangement()
// Add space from the left
pdf.addHorizontalSpace(60)            

// now add your text, table, image, ...

// finishe the horizontal arrangement so you can continue vertically
pdf.endHorizontalArrangement()

// adds a vertical space
pdf.addVerticalSpace(70)

Table Definitions

Define the layout of tables with definitions

let tableDef = TableDefinition(alignments: [.left, .left],
                               columnWidths: [100, 300],
                               fonts: [UIFont.systemFont(ofSize: 20),
                                       UIFont.systemFont(ofSize: 16)],
                               textColors: [UIColor.black,
                                            UIColor.blue])
         
let data = [] // my data
         
pdf.addTable(data.count, 
             columnCount: 2, 
             rowHeight: 25, 
             tableLineWidth: 0, // this is taken from the definition
             tableDefinition: tableDef, 
             dataArray: data)

Utilities

pdf.beginNewPage() // Begin a new page

These following commands will affect everything you write after you call.

pdf.setContentAlignment(.Center) // .Left, .Center, .Right

Generate PDF data

let pdfData = pdf.generatePDFdata()

// write to file
try? pdfData.writeToFile(path, options: .DataWritingAtomic)

// or upload to Internet
// For example, Alamofire.upload(...)

License

SimplePDF is available under the MIT License.

Authors

Nutchaphon Rewik

my twitter

GitHub

https://github.com/nRewik/SimplePDF
Comments
  • 1. Table extensions

    Hi, first of all, great library.

    I'm working a lot with tables and would like to make some extensions there. First the content alignment is not taken into account for table which means text is always centered. Second it would be great to pass attributed text to a table which give great flexibility in displaying.

    If you don't mind I will implement this and create a PR?

    Reviewed by mrichtsfeld at 2017-01-13 09:05
  • 2. Issue with compile errors

    I added this to my project, but I m getting compile errors in SimplePDF.swift => drawtext

        let attributes: [NSAttributedStringKey: Any] = [
            .font: font,
            .foregroundColor: textColor,
            .paragraphStyle: paragraphStyle
        ]
    

    Type 'NSString' has no member 'font'

        let attributes: [NSAttributedStringKey: Any] = [
            .foregroundColor: textColor,
            .paragraphStyle: paraStyle,
            .obliqueness: skew,
            .font: font
        ]
    

    Type 'NSString' has no member 'foregroundColor'

    Any clues?

    Reviewed by f15radar at 2018-01-30 03:46
  • 3. Alignments don't work in a table

    Hi there and thank you for this great library.

    I noticed that alignments don't work when creating tables. It is always aligned to the left. Trying center and right has no effect.

    Just following the example from the main page:

    let tableDef = TableDefinition(alignments: [.center, .right],
                                            columnWidths: [200, 200],
                                            fonts: [UIFont.systemFont(ofSize: 16), UIFont.systemFont(ofSize: 16)],
                                            textColors: [UIColor.black, UIColor.blue])
    
    
            let data = [["test1","test1"]]
    
            pdf.addTable(data.count,
                         columnCount: 2,
                         rowHeight: 25,
                         tableLineWidth: 0, // this is taken from the definition
                tableDefinition: tableDef,
                dataArray: data)
    

    I'm using 4.5.1 from cocoapods. Any ideas?

    Reviewed by ozitrance at 2017-12-22 07:36
  • 4. Podspec platform

    I am currently working on a project with a minimum platform target of iOS 8.0 and need to generate a PDF. I found this library and tried to install it via cocoapods and found that i was unable to do so because of the platform in your podspec is :ios, "9.0".

    So, I just tried to add the file SimplePDF.swift to the project and use it and found I was able to compile and use your library without problems.

    Could you set your podspec's platform to `:ios, "8.0"``?

    Reviewed by jaceee at 2016-02-18 16:53
  • 5. Different alignment of content in a cells

    Added content alignment from the table definition. If the definition of the table is not defined, then the content equalization is taken from the property transferred to the function.

    Reviewed by Belzik at 2018-03-31 14:26
  • 6. How to add a digital signature field

    Is there a pdf.addDigitalSignatureField option? If not, is there any way to improvise this?

    My app is programmatically generating a work contract for construction business owners to automatically email to their potential customer and have a field for customer to digitally sign, save, and return back via email.

    Thanks!!!

    Reviewed by caldwbr at 2020-01-27 00:39
  • 7. How to begin new page automatically

    Hi there,

    how can we determine page height according to content, basically i have define pagesize but unable to begin new page as data is overlapping in single page

    can you help me out how can start new page automatically

    Reviewed by hitesh-shukla at 2019-05-22 06:00
  • 8. Support for macOS

    Since a pdf generation tool like SimplePDF is quite general, I thought if it might be possible to make it compatible with macOS, and not only iOS?

    If I add the project to a macOS project, I get the following error.

    The platform of the target `TARGET NAME` (macOS 10.10) is not compatible with `SimplePDF (3.0.0)`, which does not support `osx`.

    Reviewed by viktorstrate at 2019-01-15 13:51
  • 9. How to add multiple images in a single row?

    In Pdf I have add lot of images, It displays vertically column vice but I want to add images horizontally row vice. It is possible to add images in single row.?

    Reviewed by Saravana181187 at 2018-09-06 08:01
Related tags
A simple generator of PDF written in Swift.

Features | Requirements | Installation | Usage | Communication | LICENSE PDFGenerator PDFGenerator is a simple PDF generator that generates with UIVie

May 31, 2022
An iOS PDF viewer and annotator written in Swift that can be embedded into any application.

Requirements iOS 9 or above Xcode 8 or above Swift 3.0 Note This project is still in early stages. Right now the PDF reader works both programmaticall

May 30, 2022
PdfBuilder: a swift library made to make creation of the Pdf file from code simpler
PdfBuilder: a swift library made to make creation of the Pdf file from code simpler

PdfBuilder PdfBuilder is a swift library made to make creation of the Pdf file f

Mar 7, 2022
Draw Week Time Table on PDF using PDFKit in iOS Swift
Draw Week Time Table on PDF using PDFKit in iOS Swift

DrawPDFTimeTable Draw Week Time Table on PDF using PDFKit in iOS Swift. Image Info This is the pdf of time table drawn using PDFKit in iOS Swift with

May 31, 2022
Swift package that uses WebKit to render PDF files from URLs

Swift package for generating a PDF file from a URL (rendered by WebKit)

Feb 25, 2022
PDF Reader Core for iOS
PDF Reader Core for iOS

PDF Reader Core for iOS This project is no longer supported or maintained. It is only here for historical reasons. Please see the UXReader PDF Framewo

Jun 17, 2022
PDF generator using UIViews or UIViews with an associated XIB

Description Create UIView objects using any method you like, including interface builder with Auto-layout and size classes enabled. Then generate a PD

Aug 28, 2020
Generate beautiful .pdf Files from xib

Description The Library generates a PDF directly from interface builder with Auto-layouted views! Swift Version of UIView_2_PDF. Installation Download

Sep 23, 2021
TPPDF is a simple-to-use PDF builder for iOS
TPPDF is a simple-to-use PDF builder for iOS

TPPDF is a fast PDF builder for iOS & macOS using simple commands to create advanced documents! Created and maintained by Philip Niedertscheider and a

Jun 10, 2022
UIImage PDF extensions.

UIImagePlusPDF UIImage extensions to use PDF files. Using UIImagePlusPDF you can avoid a lot png images files (1x, 2x, 3x sizes) and simply replace ea

Jan 25, 2022
Estrutura Simples para Navegacao Web e Download PDF

Download-PDF-WebView Projeto desenvolvido em Swift com a função de criar uma estrutura simples para navegação Web em seu Aplicativo, permitindo a visu

Nov 30, 2021
Mephisto - A command line tool to convert Comic Book Zip archives to PDF and share them over AirDrop
Mephisto - A command line tool to convert Comic Book Zip archives to PDF and share them over AirDrop

mephisto A command line tool written in Swift to convert Comic Book Zip archives

Feb 7, 2022
Small utility to import PDF slides as vector images into Keynote for iOS.
Small utility to import PDF slides as vector images into Keynote for iOS.

Small utility to import PDF files into Keynote for iOS. This utility is especially helpful when presenting slideshows created by LaTeX

Jun 14, 2022
📚 A Swift ePub reader and parser framework for iOS.
📚 A Swift ePub reader and parser framework for iOS.

FolioReaderKit is an ePub reader and parser framework for iOS written in Swift. Features ePub 2 and ePub 3 support Custom Fonts Custom Text Size Text

Jun 19, 2022
A Static Library to be embedded on iOS applications to display pdf documents derived from Fast PDF

FastPdfKit This repository contains the FastPdfKit iOS library with some sample projects. FastPdfKit is a library that let you show pdf documents in i

Jun 17, 2022
A simple generator of PDF written in Swift.

Features | Requirements | Installation | Usage | Communication | LICENSE PDFGenerator PDFGenerator is a simple PDF generator that generates with UIVie

May 31, 2022
An iOS PDF viewer and annotator written in Swift that can be embedded into any application.

Requirements iOS 9 or above Xcode 8 or above Swift 3.0 Note This project is still in early stages. Right now the PDF reader works both programmaticall

May 30, 2022
RadialMenu is a custom control for providing a touch context menu (like iMessage recording in iOS 8) built with Swift & POP
RadialMenu is a custom control for providing a touch context menu (like iMessage recording in iOS 8) built with Swift & POP

RadialMenu Looking for help? For $150/hr I'll help with your RadialMenu problems including integrating it into your project. Email [email protected] t

May 23, 2022
A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.
A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.

Cadmium is a Core Data framework for Swift that enforces best practices and raises exceptions for common Core Data pitfalls exactly where you make the

May 17, 2022
A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.
A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.

Cadmium is a Core Data framework for Swift that enforces best practices and raises exceptions for common Core Data pitfalls exactly where you make them.

May 17, 2022