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, height: 100)),
.Image(image1)
.Image(image2)
.ImagePath(lastPageImagePath)
.WhitePage(CGSize(width: 200,height: 100))
]
let path = NSHomeDirectory().stringByAppendingString("/sample1.pdf")
try PDFGenerator.generate(page, outputPath: path, password: "123456")
} catch let error{
print(error)
}
- 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(
NSData
) 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.
- iOS 8.0+
- Xcode 7.0+(Swift 2.x)
- Add the following to your Cartfile:
github 'sgr-ksmt/PDFGenerator' ~> 1.3.0
- Run command
- for Swift 2.2 :
carthage update
- for Swift 2.3 :
carthage update --no-use-binaries
- for Swift 2.2 :
- 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.3.0'
and run pod install
github 'sgr-ksmt/PDFGenerator' 'swift-3.0'
pod 'PDFGenerator', :branch => 'swift-3.0'
Notice : This branch is beta yet. If you found a bug, please create issue. π
- 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)
}
}
// generate dpi300 PDF (default: 72dpi)
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 pages = [page1, page2]
let dst = NSHomeDirectory().stringByAppendingString("/sample1.pdf")
do {
try PDFGenerator.generate(pages, outputPath: dst, dpi: .DPI_300)
} catch (let e) {
print(e)
}
}
// generate PDF with password: 123456
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 pages = [page1, page2]
let dst = NSHomeDirectory().stringByAppendingString("/sample1.pdf")
do {
try PDFGenerator.generate(pages, outputPath: dst, password: "123456")
// or use PDFPassword model
try PDFGenerator.generate(pages, outputPath: dst, password: PDFPassword("123456"))
// or use PDFPassword model and set user/owner password
try PDFGenerator.generate(pages, outputPath: dst, password: PDFPassword(user: "123456", owner: "abcdef"))
} 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.
}
- 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:
PDFGenerator is under MIT license. See the LICENSE file for more info.