All Projects → maximbilan → Ios Uiimage Render To Pdf

maximbilan / Ios Uiimage Render To Pdf

Licence: mit
iOS Render UIImage to PDF and merging PDF files

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Ios Uiimage Render To Pdf

Ethereum Development With Go Book
📖 A little book on Ethereum Development with Go (golang)
Stars: ✭ 754 (+9325%)
Mutual labels:  tutorial, pdf
Sign In With Apple Swift Example
iOS + Node.js authentication using Sign in with Apple
Stars: ✭ 27 (+237.5%)
Mutual labels:  tutorial
Excalibur
A web interface to extract tabular data from PDFs
Stars: ✭ 916 (+11350%)
Mutual labels:  pdf
R Notes
Notes for using R language to do data mining and machine learning (Chinese)
Stars: ✭ 25 (+212.5%)
Mutual labels:  tutorial
Fastapi Realworld Example App
Backend logic implementation for https://github.com/gothinkster/realworld with awesome FastAPI
Stars: ✭ 911 (+11287.5%)
Mutual labels:  tutorial
Osgi For Mere Mortals
Sample code for my "OSGi for mere mortals" presentation at ApacheCon NA 2011
Stars: ✭ 25 (+212.5%)
Mutual labels:  tutorial
Tic Tac Toe
An example tutorial built with git-tutor https://github.com/lesnitsky/git-tutor
Stars: ✭ 22 (+175%)
Mutual labels:  tutorial
Curriculum
📝 A simple way to create your HTML résumé.
Stars: ✭ 7 (-12.5%)
Mutual labels:  pdf
Rmarkdown Website Tutorial
Tutorial for creating websites w/ R Markdown
Stars: ✭ 26 (+225%)
Mutual labels:  tutorial
Elm Cheat Sheet
An overview of Elm syntax and features
Stars: ✭ 928 (+11500%)
Mutual labels:  tutorial
Python Introducing Pandas
Introduction to pandas Treehouse course
Stars: ✭ 24 (+200%)
Mutual labels:  tutorial
Har Keras Coreml
Human Activity Recognition (HAR) with Keras and CoreML
Stars: ✭ 23 (+187.5%)
Mutual labels:  tutorial
Amazing Books
编程电子书思维导图及下载链接
Stars: ✭ 26 (+225%)
Mutual labels:  pdf
Itext7
iText 7 for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText 7 can be a boon to nearly every workflow.
Stars: ✭ 913 (+11312.5%)
Mutual labels:  pdf
Probtopdf
Turn online textbook into Exam-friendly, offline, searchable PDF
Stars: ✭ 27 (+237.5%)
Mutual labels:  pdf
Synthetic Programming
Synthetic Programming code from tutorials and other videos
Stars: ✭ 21 (+162.5%)
Mutual labels:  tutorial
Algolia Swift Demo
iOS instant search tutorial
Stars: ✭ 23 (+187.5%)
Mutual labels:  tutorial
Haxejs
Documentation about using JavaScript with Haxe
Stars: ✭ 25 (+212.5%)
Mutual labels:  tutorial
Nginx Tutorial
这是一个 Nginx 极简教程,目的在于帮助新手快速入门 Nginx。
Stars: ✭ 845 (+10462.5%)
Mutual labels:  tutorial
Network Examples
Linux networking examples and tutorials
Stars: ✭ 837 (+10362.5%)
Mutual labels:  tutorial

iOS Render UIImage to PDF and merging PDF files

alt tag

Some code samples for working with PDF. Let’s try to generate 1000 images and render to PDF file. For this, we need a method for generating a random color and a method for creating UIImage from UIColor.

A random color:

- (UIColor *)randomColor
{
    CGFloat hue = (arc4random() % 256 / 256.0);
    CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5;
    CGFloat brightness = (arc4random() % 128 / 256.0 ) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

And UIImage from UIColor:

- (UIImage *)imageFromColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1024, 1024);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

And now we can render random images to PDF file.

UIGraphicsBeginPDFContextToFile(filename, CGRectMake(0, 0, 1024, 1024), nil);

for (NSInteger i = 0; i < 1000; ++i) {
    UIImage *image = [self imageFromColor:[self randomColor]];

    @autoreleasepool {
        UIGraphicsBeginPDFPage();
        [image drawAtPoint:CGPointZero];
    }
}

UIGraphicsEndPDFContext();

Attention! Necessarily use @autoreleasepool, otherwise, you will have memory leaks.

Also, I would like to provide some sample for generating PDF files and merging these files. It’s also simple.

NSMutableArray *pdfURLs = [NSMutableArray array];

for (NSInteger i = 0; i < 1000; ++i) {
    NSString *pdfFile = [NSString stringWithFormat:@”%@_%@”, filename, @(i)];
    UIImage *imageToRender = [self imageFromColor:[self randomColor]];
  
    @autoreleasepool {
        UIGraphicsBeginPDFContextToFile(pdfFile, CGRectMake(0, 0, 1024, 1024), nil);
        UIGraphicsBeginPDFPage();
        [imageToRender drawAtPoint:CGPointZero];
        UIGraphicsEndPDFContext();
    }

    [pdfURLs addObject:[NSURL fileURLWithPath:pdfFile]];
}

[self combinePDFURLs:pdfURLs writeToURL:[NSURL fileURLWithPath:filename]];

for (NSURL *pdfUrl in pdfURLs) {
    [[NSFileManager defaultManager] removeItemAtURL:pdfUrl error:nil];
}

And of course, implementation of combinePDFURLs method, see below:

- (void)combinePDFURLs:(NSArray *)PDFURLs writeToURL:(NSURL *)URL
{
    CGContextRef context = CGPDFContextCreateWithURL((__bridge CFURLRef)URL, NULL, NULL);

    for (NSURL *PDFURL in PDFURLs) {
        CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)PDFURL); 
        size_t numberOfPages = CGPDFDocumentGetNumberOfPages(document);

        for (size_t pageNumber = 1; pageNumber <= numberOfPages; ++pageNumber) {
            CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber);
            CGRect mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
            CGContextBeginPage(context, &mediaBox);
            CGContextDrawPDFPage(context, page);
            CGContextEndPage(context);
        }

        CGPDFDocumentRelease(document);
    }

    CGPDFContextClose(context);
    CGContextRelease(context);
}

And the result:

alt tag

Happy coding!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].