• Stars
    star
    754
  • Rank 60,205 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple generator of PDF written in Swift.

Features | Requirements | Installation | Usage | Communication | LICENSE

PDFGenerator

Build Status GitHub release codecov Language Carthage CocoaPods CocoaPodsDL Awesome Reviewed by Hound

PDFGenerator is a simple PDF generator that generates with UIView, UIImage, ...etc .

do {
    let page: [PDFPage] = [
        .whitePage(CGSize(width: 200.0, height: 100.0)),
        .image(image1)
        .image(image2)
        .imagePath(lastPageImagePath)
        .whitePage(CGSize(width: 200.0, height: 100.0))
    ]
    let path = NSTemporaryDirectory().appending("sample1.pdf")
    try PDFGenerator.generate(page, to: path, password: "123456")
} catch let error {
    print(error)
}

Features

  • Swift 5 is ready 🙏
  • Multiple pages support.
  • Also generate PDF with image path, image binary, image ref (CGImage)
  • Good memory management.
  • UIScrollView support : If view is UIScrollView, UITableView, UICollectionView, UIWebView, drawn whole content.
  • Outputs as binary(Data) or writes to Disk(in given file path) directly.
  • Corresponding to Error-Handling. Strange PDF has never been generated!!.
  • DPI support. : Default dpi is 72.
  • Password protection support.

Requirements

  • iOS 9.0+
  • Xcode 11+
  • Swift 5.1

Installation

Carthage

  • Add the following to your Cartfile:
github "sgr-ksmt/PDFGenerator" ~> 3.1
  • Then run command:
$ carthage update

CocoaPods

PDFGenerator is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'PDFGenerator', '~> 3.1'

and run pod install

Usage

Generate from view(s) or image(s)

  • UIView → PDF
func generatePDF() {
    let v1 = UIScrollView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    let v3 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v1.backgroundColor = .red
    v1.contentSize = CGSize(width: 100.0, height: 200.0)
    v2.backgroundColor = .green
    v3.backgroundColor = .blue

    let dst = URL(fileURLWithPath: NSTemporaryDirectory().appending("sample1.pdf"))
    // outputs as Data
    do {
        let data = try PDFGenerator.generated(by: [v1, v2, v3])
        try data.write(to: dst, options: .atomic)
    } catch (let error) {
        print(error)
    }

    // writes to Disk directly.
    do {
        try PDFGenerator.generate([v1, v2, v3], to: dst)    
    } catch (let error) {
        print(error)
    }
}

Also PDF can generate from image(s), image path(s) same as example.

Generate from PDFPage object

  • (UIVIew or UIImage) → PDF

Use PDFPage.

public enum PDFPage {
    case whitePage(CGSize) // = A white view
    case view(UIView)
    case image(UIImage)
    case imagePath(String)
    case binary(Data)
    case imageRef(CGImage)
}
func generatePDF() {
    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    v1.backgroundColor = .red
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v2.backgroundColor = .green

    let page1 = PDFPage.View(v1)
    let page2 = PDFPage.View(v2)
    let page3 = PDFPage.WhitePage(CGSizeMake(200, 100))
    let page4 = PDFPage.Image(UIImage(contentsOfFile: "path/to/image1.png")!)
    let page5 = PDFPage.ImagePath("path/to/image2.png")
    let pages = [page1, page2, page3, page4, page5]

    let dst = NSTemporaryDirectory().appending("sample1.pdf")
    do {
        try PDFGenerator.generate(pages, to: dst)
    } catch (let e) {
        print(e)
    }
}

Generate custom dpi PDF

// generate dpi300 PDF (default: 72dpi)
func generatePDF() {
    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    v1.backgroundColor = .red
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v2.backgroundColor = .green

    let page1 = PDFPage.View(v1)
    let page2 = PDFPage.View(v2)
    let pages = [page1, page2]

    let dst = NSTemporaryDirectory().appending("sample1.pdf")
    do {
        try PDFGenerator.generate(pages, to: dst, dpi: .dpi_300)
    } catch (let e) {
        print(e)
    }
}

Password protection

// generate PDF with password: 123456
func generatePDF() {
    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    v1.backgroundColor = .red
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v2.backgroundColor = .green

    let page1 = PDFPage.view(v1)
    let page2 = PDFPage.view(v2)
    let pages = [page1, page2]

    let dst = NSTemporaryDirectory().appending("sample1.pdf")
    do {
        try PDFGenerator.generate(pages, to: dst, password: "123456")
        // or use PDFPassword model
        try PDFGenerator.generate(pages, to: dst, password: PDFPassword("123456"))
        // or use PDFPassword model and set user/owner password
        try PDFGenerator.generate(pages, to: dst, password: PDFPassword(user: "123456", owner: "abcdef"))
    } catch let error {
        print(error)
    }
}

Communication

  • If you found a bug, please open an issue. 🙇
  • Also, if you have a feature request, please open an issue. 👍
  • If you want to contribute, submit a pull request.:muscle:

License

PDFGenerator is under MIT license. See the LICENSE file for more info.

More Repositories

1

PullToDismiss

You can dismiss modal viewcontroller like Facebook Messenger by pulling scrollview or navigationbar in Swift.
Swift
485
star
2

FireTodo

Simple Todo Application using SwiftUI / Firebase / Redux.
Swift
376
star
3

Alertift

Swifty, modern UIAlertController wrapper.
Swift
283
star
4

WaterfallLayout

water-fall layout in iOS
Swift
212
star
5

FormToolbar

Simple, movable and powerful toolbar for UITextField and UITextView.
Swift
86
star
6

Lobster

🦐 Type-safe Firebase-RemoteConfig helper library 🦐
Swift
77
star
7

firebase-ec-demo

E-commerce data model, security rules example using Cloud Firestore.
58
star
8

FireSnapshot

A useful Firebase-Cloud-Firestore Wrapper with Codable.
Swift
56
star
9

Shopping-Cart

Swift
45
star
10

SwiftUI-Firebase-Todo

Swift
35
star
11

BeautifulGridLayout

UICollectionView extension for showing beautiful grid!
Swift
32
star
12

SNSShare

Text/Image/URL share to Twitter/Facebook/LINE from App in Swift.
Swift
25
star
13

Once

Execute closure just once!!! Once is a micro framework.
Objective-C
19
star
14

FirstAppearing

[Swift] Helper Protocol-Extension for UIViewController's viewWillAppear/viewDidAppear.
Swift
17
star
15

Curriculum-Vitae

16
star
16

SwiftUI-Todo

Todo application example using SwiftUI
Swift
16
star
17

flutter-github-repo-explorer

This is an example application with Flutter using river_pod, state_notifier, and freezed.
Dart
16
star
18

firestore-like-feature-example

An example of like feature such as Twitter, Instagram with Cloud Firestore
TypeScript
15
star
19

TwitterLikeHeaderExample

Swift
15
star
20

Shrimp

Shrimp is Firebase-RemoteConfig helper library.
Swift
14
star
21

BlendingCulturesExample

Example for "Blending Cultures" of try! Swift's Presentation
Swift
12
star
22

cloud-functions-practical-configuration

TypeScript
11
star
23

RxFirebaseAuth

Combination of RxSwift and Firebase Authentication
Swift
11
star
24

DTFont

DynamicTypeFont helper. Easy creation, custom font etc.
Swift
10
star
25

firebase-recommended-template

The recommended template for developing with Firebase
HTML
9
star
26

Quiche

Type-safe Firestore query helper for Swift.
Swift
7
star
27

firestore-emulator-rules-test

Test Firestore rules using jest and firestore-emulator
TypeScript
6
star
28

RxPlayground

Xcode Playground with RxSwift
Swift
6
star
29

APIKitWithDecodableSample

APIKit x Swift.Decodable sample
Swift
5
star
30

Border

Border is CloudFunction libarry that connect Firebase and Stripe.
JavaScript
5
star
31

ApplicationEventObserver

Application notification event handling in Swift
Swift
5
star
32

i18nSwift

i18n for Swift
Swift
4
star
33

firebase-storage-rule-test-example

An Example of writing the test of storage.rules
TypeScript
4
star
34

firestore-rules-test-example

TypeScript
4
star
35

dotfiles

Shell
3
star
36

ElastiQ

Generate ElasticSearch query in Swift
Swift
3
star
37

SourceryDemo

Demo using Sourcery for auto-generating boiler-plate of `LogEvent`.
Swift
3
star
38

FDLBuilder

Firebase Dynamic Links builder
Swift
3
star
39

FormChangeable

Easy to move from UITextField/UITextView to next UITextField/UITextView written in Swift.
Swift
2
star
40

CloudFirestoreTestExample

Example for Cloud Firestore online test with jest.
TypeScript
2
star
41

EnhancedAutoReleasePool

EnhancedAutoReleasePool is μ-Library for autoreleasepool.
Swift
2
star
42

Pick

Protocol-Oriented PickerViewController
Swift
2
star
43

SUCheckButton

light-weight check button in Swift.
Swift
1
star
44

EnvToSwift

Config file generator from `.env`
Shell
1
star
45

FirebaseABTestingExample

Firebase A/B Testing Example
Swift
1
star
46

flutter_test_report_on_github_action_example

Dart
1
star
47

plain-emulator-sandbox

TypeScript
1
star
48

CloudFunctionsAPI

Provides secure request between client and Cloud Functions
Swift
1
star
49

my-styleguide

My coding styleguide
1
star
50

AutoIncrement_id_Realm_Example

Example of auto increment `id` in Realm
Ruby
1
star