Skip to content

Latest commit

 

History

History
243 lines (170 loc) · 8.77 KB

README.md

File metadata and controls

243 lines (170 loc) · 8.77 KB

BNHtmlPdfKit

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!

Adding BNHtmlPdfKit To Your Project

Just copy BNHtmlPdfKit.h and BNHtmlPdfKit.m into your project.

CocoaPods

pod 'BNHtmlPdfKit', :git => 'https://github.com/brentnycum/BNHtmlPdfKit'

Background

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.

Usage

You can either use blocks based or delegate based calling.

Blocks

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");

}];

Delegates

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 {
	...
}

Initializers

- (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.

Saving a URL

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"]];

Saving an HTML String

BNHtmlPdfKit *htmlPdfKit = [[BNHtmlPdfKit alloc] init];
htmlPdfKit.delegate = self;
[htmlPdfKit saveHtmlAsPdf:@"<html>..." toFile:@"...itsbrent.pdf"];

To just save PDF data.

[htmlPdfKit saveHtmlAsPdf:@"<html>..."];

Delegate Methods

- (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.

Page Sizes

BNHtmlPdfKit has support for many of the top paper sizes.

  • Letter - BNPageSizeLetter
  • Government Letter - BNPageSizeGovernmentLetter
  • Legal - BNPageSizeLegal
  • Junior Legal - BNPageSizeJuniorLegal
  • Ledger - BNPageSizeLedger
  • Tabloid - BNPageSizeTabloid
  • A0 - BNPageSizeA0
  • A1 - BNPageSizeA1
  • A2 - BNPageSizeA2
  • A3 - BNPageSizeA3
  • A4 - BNPageSizeA4
  • A5 - BNPageSizeA5
  • A6 - BNPageSizeA6
  • A7 - BNPageSizeA7
  • A8 - BNPageSizeA8
  • A9 - BNPageSizeA9
  • A10 - BNPageSizeA10
  • B0 - BNPageSizeB0
  • B1 - BNPageSizeB1
  • B2 - BNPageSizeB2
  • B3 - BNPageSizeB3
  • B4 - BNPageSizeB4
  • B5 - BNPageSizeB5
  • B6 - BNPageSizeB6
  • B7 - BNPageSizeB7
  • B8 - BNPageSizeB8
  • B9 - BNPageSizeB9
  • B10 - BNPageSizeB10
  • C0 - BNPageSizeC0
  • C1 - BNPageSizeC1
  • C2 - BNPageSizeC2
  • C3 - BNPageSizeC3
  • C4 - BNPageSizeC4
  • C5 - BNPageSizeC5
  • C6 - BNPageSizeC6
  • C7 - BNPageSizeC7
  • C8 - BNPageSizeC8
  • C9 - BNPageSizeC9
  • C10 - BNPageSizeC10
  • Japanese B0 - BNPageSizeJapaneseB0
  • Japanese B1 - BNPageSizeJapaneseB1
  • Japanese B2 - BNPageSizeJapaneseB2
  • Japanese B3 - BNPageSizeJapaneseB3
  • Japanese B4 - BNPageSizeJapaneseB4
  • Japanese B5 - BNPageSizeJapaneseB5
  • Japanese B6 - BNPageSizeJapaneseB6
  • Japanese B7 - BNPageSizeJapaneseB7
  • Japanese B8 - BNPageSizeJapaneseB8
  • Japanese B9 - BNPageSizeJapaneseB9
  • Japanese B10 - BNPageSizeJapaneseB10
  • Japanese B11 - BNPageSizeJapaneseB11
  • Japanese B12 - BNPageSizeJapaneseB12

Custom Page Sizes

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);

Landscape Support

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;

Margin Sizes

Default margin size is set to 1/4".

htmlPdfKit.topAndBottomMarginSize = 0.25f * 72.0f;
htmlPdfKit.leftAndRightMarginSize = 0.25f * 72.0f;

Todo

  • Fix page sizes to not use rounded inch values.
  • Custom Top, Left, Bottom, and Right margins

Contact

Thanks