Swiftpack.co - Package - janigro/QR2Pdf

MIT License Swift Package Manager Compatible Swift 5.0


This package offers an extension to the QRCode class, and enables you to create a pdf that can be either saved to a file or to a Data object. This package is cross-platform. The only dependencies are shown below.


There is only one dependency, The QR2Pdf package needs to use the QRCode class, which is part of the package swift_qrcodejs. For help on how to create a QRCode, check its repository: https://github.com/ApolloZhu/swift_qrcodejs

How to setup

Add to your Package.swift, the following:

.package(url: "https://github.com/janigro/QR2Pdf.git", from: "1.0.0")

The package name is QR2Pdf, added it to your target, for example:

.target(name: "myapp", dependencies: [ "QR2Pdf" ]),

Basic Usage

The extension can be configured with page size, colors, and other parameters. However, using the defaults, it will generate a square pdf of 7cm x 7cm, with a QR code of 5cm x 5cm and centered:

guard let qr = QRCode("hello world!") else { return }

// Save as a PDF file
try? qr.pdf().save(url: URL(fileURLWithPath: "/path/filename.pdf"))

// Store the PDF into a Data object
let data = qr.pdf().data()

Advanced Usage

The following examples, shows you have to specify colors and sizes, both for the page and the QR code.

guard let qr = QRCode("hello world!") else { return }

let properties = QRProperties(onColor: .black, offColor: .red, dimension: .cm(left: 2, bottom: 2, size: 10))
let pdf = qr.pdf(pageSize: .A4, properties: properties, bgColor: .blue)

try? qr.pdf().save(url: URL(fileURLWithPath: "/path/filename.pdf"))


Colors can be specified in RGB, Grayscale, or using the existing shorcuts:

    let color: PDFColor = .rgb(0, 0.4, 1)
    let color: PDFColor = .gray(0.5)
    let color: PDFColor = .black
    let color: PDFColor = .white
    let color: PDFColor = .red
    let color: PDFColor = .green
    let color: PDFColor = .blue
    let color: PDFColor = .yellow
    let color: PDFColor = .orange

Page Size

Page sizes can be specify in cm, inches or points (1/72 of an inch). There are also some shortcuts:

let pagesize: PDFPageSize = .A4
let pagesize: PDFPageSize = .Letter
let pagesize: PDFPageSize = .QR // A square pdf of 7cm x 7cm
let pagesize: PDFPageSize = .cm(width: 10, height: 20)
let pagesize: PDFPageSize = .inches(width: 4, height: 6)
let pagesize: PDFPageSize = .pt(width: 300, height: 400)

QR Properties

Several characteristics of the QR code can be configured as shown below. The onColor is used for the dots of the QR code, the offColor is used for its background and the dimension determines its size and position

    let props = QRProperties(onColor: .black, offColor: .white, dimension: loc)

The size and position can be configured with multiple units:

    let loc: QRDimension = .cm(left: 1, bottom: 1, size: 5)
    let loc: QRDimension = .inches(left: 3, bottom: 7, size: 4)
    let loc: QRDimension = .pt(left: 10, bottom: 20, size: 100)
    let loc: QRDimension = .QR // 5cm x 5cm at (1,1)

Adding text

There is limited support for adding text. Text can be positioned behind, or in front of the QR code.

When calling pdf(), you can provide two arrays. One with all labels to add behind and the other with all the labels to add in fron of the QR code:

let pdf = qr.pdf(behindText: bTexts, frontText: fTexts)

Each text array element has a string, a color, a position and a font size. Font is always Helvetica.

    let fTexts:[PDFText] = [
        PDFText(text: "First Label", color: .green, position: .cm(x: 0, y: 4), size: 16),
        PDFText(text: "Second Label", color: .orange, position: .cm(x: 0, y: 5), size: 18)


I am not a PDF spec expert. Until yesterday I knew nothing about it. This means that if you need more fancy options to create your PDF, you should probably link to a serious pdf library. However, if you would like a very light weight option, go ahead an clone away.


Stars: 0
Help us keep the lights on

Used By

Total: 0


1.0.1 - May 19, 2019

1.0.0 - May 19, 2019

0.9.0 - May 19, 2019