BNHtmlPdfKit easily turns HTML data from an HTML string or URL into a PDF file on iOS. Feel free to fork this project and help make it better!
If you are using BNHtmlPdfKit in your app I would love to hear about it!
Just copy BNHtmlPdfKit.h
and BNHtmlPdfKit.m
into your project.
pod 'BNHtmlPdfKit', :git => 'https://github.com/brentnycum/BNHtmlPdfKit'
This all started with a post of mine back in June of 2011, when I was trying to save Html data to a PDF and copy the PDF data to an email as an attachment. I had always wanted to make the code better and was finally able to. The post of mine generates a bunch of traffic to my blog to this day, by an order of 10 fold to the next top page and is still a very popular problem that people are working on.
You can either use blocks based or delegate based calling.
There are quite a few block methods provided for convenience.
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url success:(void (^)(NSData *pdfData))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url pageSize:(BNPageSize)pageSize success:(void (^)(NSData *pdfData))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url pageSize:(BNPageSize)pageSize isLandscape:(BOOL)landscape success:(void (^)(NSData *pdfData))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url pageSize:(BNPageSize)pageSize isLandscape:(BOOL)landscape topAndBottomMarginSize:(CGFloat)topAndBottom leftAndRightMarginSize:(CGFloat)leftAndRight success:(void (^)(NSData *pdfData))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)filename success:(void (^)(NSString *filename))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)filename pageSize:(BNPageSize)pageSize success:(void (^)(NSString *filename))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)filename pageSize:(BNPageSize)pageSize isLandscape:(BOOL)landscape success:(void (^)(NSString *filename))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)filename pageSize:(BNPageSize)pageSize isLandscape:(BOOL)landscape topAndBottomMarginSize:(CGFloat)topAndBottom leftAndRightMarginSize:(CGFloat)leftAndRight success:(void (^)(NSString *filename))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url customPageSize:(CGSize)pageSize success:(void (^)(NSData *pdfData))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url customPageSize:(CGSize)pageSize topAndBottomMarginSize:(CGFloat)topAndBottom leftAndRightMarginSize:(CGFloat)leftAndRight success:(void (^)(NSData *pdfData))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)filename customPageSize:(CGSize)pageSize success:(void (^)(NSString *filename))completion failure:(void (^)(NSError *error))failure;
+ (BNHtmlPdfKit *)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)filename customPageSize:(CGSize)pageSize topAndBottomMarginSize:(CGFloat)topAndBottom leftAndRightMarginSize:(CGFloat)leftAndRight success:(void (^)(NSString *filename))completion failure:(void (^)(NSError *error))failure;
To use them simply call:
htmlPdfKit = [BNHtmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://itsbrent.net"] toFile:@"...itsbrent.pdf" pageSize:BNPageSizeA6 success:^(NSString *pdfFileName) {
NSLog(@"Done");
} failure:^(NSError *err) {
NSLog(@"Failure");
}];
Be sure to retain a reference to the BNHtmlPdfKit
object outside the scope of the calling method. Otherwise, no delegate methods will be called:
- (void) createPdf:(id)sender {
_htmlPdfKit = [[BNHtmlPdfKit alloc] init];
_htmlPdfKit.delegate = self;
[_htmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://itsbrent.net/index.html"]];
}
...
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didSavePdfData:(NSData *)data {
...
}
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didSavePdfFile:(NSString *)file {
...
}
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didFailWithError:(NSError *)error {
...
}
- (id)init;
- (id)initWithPageSize:(BNPageSize)pageSize;
- (id)initWithCustomPageSize:(CGSize)pageSize;
Default initializer has default page size based on locale (thanks Pierre Bernard) and 1/4" margins.
BNHtmlPdfKit *htmlPdfKit = [[BNHtmlPdfKit alloc] init];
htmlPdfKit.delegate = self;
[htmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://itsbrent.net"] toFile:@"...itsbrent.pdf"];
To just save PDF data.
[htmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://itsbrent.net"]];
BNHtmlPdfKit *htmlPdfKit = [[BNHtmlPdfKit alloc] init];
htmlPdfKit.delegate = self;
[htmlPdfKit saveHtmlAsPdf:@"<html>..." toFile:@"...itsbrent.pdf"];
To just save PDF data.
[htmlPdfKit saveHtmlAsPdf:@"<html>..."];
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didSavePdfData:(NSData *)data;
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didSavePdfFile:(NSString *)file;
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didFailWithError:(NSError *)error;
didSavePdfData
is called whenever PDF data is generated for an Html string or URL. didSavePdfFile
is called whenever a PDF file was saved using the toFile
methods.
BNHtmlPdfKit has support for many of the top paper sizes.
BNPageSizeLetter
BNPageSizeGovernmentLetter
BNPageSizeLegal
BNPageSizeJuniorLegal
BNPageSizeLedger
BNPageSizeTabloid
BNPageSizeA0
BNPageSizeA1
BNPageSizeA2
BNPageSizeA3
BNPageSizeA4
BNPageSizeA5
BNPageSizeA6
BNPageSizeA7
BNPageSizeA8
BNPageSizeA9
BNPageSizeA10
BNPageSizeB0
BNPageSizeB1
BNPageSizeB2
BNPageSizeB3
BNPageSizeB4
BNPageSizeB5
BNPageSizeB6
BNPageSizeB7
BNPageSizeB8
BNPageSizeB9
BNPageSizeB10
BNPageSizeC0
BNPageSizeC1
BNPageSizeC2
BNPageSizeC3
BNPageSizeC4
BNPageSizeC5
BNPageSizeC6
BNPageSizeC7
BNPageSizeC8
BNPageSizeC9
BNPageSizeC10
BNPageSizeJapaneseB0
BNPageSizeJapaneseB1
BNPageSizeJapaneseB2
BNPageSizeJapaneseB3
BNPageSizeJapaneseB4
BNPageSizeJapaneseB5
BNPageSizeJapaneseB6
BNPageSizeJapaneseB7
BNPageSizeJapaneseB8
BNPageSizeJapaneseB9
BNPageSizeJapaneseB10
BNPageSizeJapaneseB11
BNPageSizeJapaneseB12
BNHtmlPdfKit also supports custom page sizes by using the customPageSize
property. Specify your page size in inches * 72.0f.
htmlPdfKit.customPageSize = CGSizeMake(8.5f * 72.0f, 11.0f * 72.0f);
Support for setting a paper size and setting as landscape is available by either using the landscape
property or by using the custom init method below.
- (id)initWithPageSize:(BNPageSize)pageSize isLandscape:(BOOL)landscape;
Default margin size is set to 1/4".
htmlPdfKit.topAndBottomMarginSize = 0.25f * 72.0f;
htmlPdfKit.leftAndRightMarginSize = 0.25f * 72.0f;