Giter Site home page Giter Site logo

bnhtmlpdfkit's Introduction

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

Initializers

- (id)init;
- (id)initWithPageSize:(BNPageSize)pageSize;
- (id)initWithCustomPageSize:(CGSize)pageSize;

Default initializer has default page size of letter 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);

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

bnhtmlpdfkit's People

Contributors

brentnycum avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.