PrintingKit is a Swift SDK that helps you print images, strings, views, PDFs etc. in Swift and SwiftUI.
PrintingKit is a SwiftUI library that lets you print images, strings, views, files, PDFs, etc. from any SwiftUI app. Just create a Printer
instance or use the Printer.shared
printer, then call any of its print functions to print.
PrintingKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/PrintingKit.git
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
To print, just create a Printer
instance, or use the Printer.shared
printer, then use it to print any of the following supported printable types:
printAttributedString(_:config:)
- print an attributed string.printData(_:withFileExtension:)
- try to print generic data.printFile(at:)
- try to print a generic file.printImage(_:)
- print a UIImage
or NSImage
.printImageData(_:)
- print JPG or PNG data.printImageFile(at:)
- print a JPG or PNG file at a certain URL.printPdfData(_:)
- print PDF document data.printPdfFile(at:)
- print a PDF document file at a certain URL.printString(_:config:)
- print a plain string.printView(_:withScale:)
- print a SwiftUI view.In SwiftUI, you can either print programatically, when a user taps/clicks a button, etc.:
struct MyView: View {
var body: some View {
VStack {
Button("Print something") {
do {
try? Printer.shared.printString("Hello, world!")
} catch {
print("Handle this \(error)")
}
}
}
}
}
PrintingKit also has PDF utilities, which are used to print certain types. Since these utilies are the only ones that support paper size, page margins, etc. we should aim to make more print functions use PDF as print format.
Note: Only some functions support providing a custom page configuration, which can be used to specify paper size and margins. More functions should support this functionality in the future.
For a sandboxed application (default on macOS), you must allow printing in the target’s “Signing & Capabilities” > “App Sandbox” section or, you’ll be met with the error “This application does not support printing.”.
The online documentation has more information, articles, code examples, etc.
The Demo
folder has a demo app that lets you explore the library.
Feel free to reach out if you have questions, or want to contribute in any way:
PrintingKit is available under the MIT license. See the LICENSE file for more info.