A simple PDF generator.
Generate PDF from UIView/UIImage (single page or multiple page).
- Support multiple pages.
- Also generate PDF from:
- ImagePath : that can load image with
UIImage(contentsOfFile:)
- Binary(
NSData
) - ImageRef(
CGImage
)
- ImagePath : that can load image with
- Type safe.
- Good memory management.
- Generate PDF from mixed-pages.
- If view is
UIScrollView
,UITableView
,UICollectionView
,UIWebView
, drawn whole content. - Outputs as
NSData
or writes to Disk(in given file path) directly. - Corresponding to Error-Handling. Strange PDF has never been generated!!
- UIView → PDF
func generatePDF() {
let v1 = UIScrollView(frame: CGRectMake(0,0,100,100))
let v2 = UIView(frame: CGRectMake(0,0,100,200))
let v3 = UIView(frame: CGRectMake(0,0,100,200))
v1.backgroundColor = UIColor.redColor()
v1.contentSize = CGSize(width: 100, height: 200)
v2.backgroundColor = UIColor.greenColor()
v3.backgroundColor = UIColor.blueColor()
let dst = NSHomeDirectory().stringByAppendingString("/sample1.pdf")
// outputs as NSData
do {
let data = try PDFGenerator.generate([v1, v2, v3])
data.writeToFile(dst, atomically: true)
} catch (let error) {
print(error)
}
// writes to Disk directly.
do {
try PDFGenerator.generate([v1, v2, v3], outputPath: dst)
} catch (let error) {
print(error)
}
}
Also PDF can generate from image(s), image path(s) same as example.
- (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(NSData)
case ImageRef(CGImage)
}
func generatePDF() {
let v1 = UIView(frame: CGRectMake(0,0,100,100))
v1.backgroundColor = UIColor.redColor()
let v2 = UIView(frame: CGRectMake(0,0,100,200))
v2.backgroundColor = UIColor.greenColor()
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 = NSHomeDirectory().stringByAppendingString("/sample1.pdf")
do {
try PDFGenerator.generate(pages, outputPath: dst)
} catch (let e) {
print(e)
}
}
public enum PDFGenerateError: ErrorType {
case ZeroSizeView(UIView) // view's width or height is 0
case ImageLoadFailed(String) // cannot load from image path
case EmptyOutputPath // output path is empty
case EmptyPage // Generate from empty page(s).
// `PDFGenerator.generate([])` isn't allowed.
}
- iOS 8.0+
- Xcode 7.0+(Swift 2.x)
- Add the following to your Cartfile:
github 'sgr-ksmt/PDFGenerator' ~> 1.1.3
- Run
carthage update
- Add the framework as described.
Details: Carthage Readme
PDFGenerator is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'PDFGenerator', '~> 1.1.3'
and run pod install
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.:muscle:
PDFGenerator is under MIT license. See the LICENSE file for more info.