Features | Requirements | Installation | Usage | Communication | LICENSE
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)
}
image path
, image binary
, image ref (CGImage)
UIScrollView
, UITableView
, UICollectionView
, UIWebView
, drawn whole content.Data
) or writes to Disk(in given file path) directly.github "sgr-ksmt/PDFGenerator" ~> 3.1
$ carthage update
PDFGenerator is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'PDFGenerator', '~> 3.1'
and run pod install
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.
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 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)
}
}
// 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)
}
}
PDFGenerator is under MIT license. See the LICENSE file for more info.