{"id":18839760,"url":"https://github.com/maximbilan/ios-uiimage-render-to-pdf","last_synced_at":"2025-04-14T06:55:14.853Z","repository":{"id":76126034,"uuid":"45641240","full_name":"maximbilan/iOS-UIImage-render-to-PDF","owner":"maximbilan","description":"iOS Render UIImage to PDF and merging PDF files","archived":false,"fork":false,"pushed_at":"2018-09-22T09:08:59.000Z","size":603,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T06:55:10.403Z","etag":null,"topics":["ios","pdf","render","swift","tutorial","uicolor","uiimage"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximbilan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-05T21:42:22.000Z","updated_at":"2025-02-06T04:17:27.000Z","dependencies_parsed_at":"2023-02-27T01:01:12.668Z","dependency_job_id":null,"html_url":"https://github.com/maximbilan/iOS-UIImage-render-to-PDF","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-UIImage-render-to-PDF","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-UIImage-render-to-PDF/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-UIImage-render-to-PDF/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-UIImage-render-to-PDF/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximbilan","download_url":"https://codeload.github.com/maximbilan/iOS-UIImage-render-to-PDF/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837281,"owners_count":21169374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","pdf","render","swift","tutorial","uicolor","uiimage"],"created_at":"2024-11-08T02:44:00.149Z","updated_at":"2025-04-14T06:55:14.844Z","avatar_url":"https://github.com/maximbilan.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# iOS Render UIImage to PDF and merging PDF files\n\n![alt tag](https://raw.github.com/maximbilan/iOS-UIImage-render-to-PDF/master/img/img1.png)\n\nSome code samples for working with \u003ci\u003ePDF\u003c/i\u003e. Let’s try to generate 1000 images and render to \u003ci\u003ePDF\u003c/i\u003e file. For this, we need a method for generating a random color and a method for creating \u003ci\u003eUIImage\u003c/i\u003e from \u003ci\u003eUIColor\u003c/i\u003e.\n\nA random color:\n\n\u003cpre\u003e\n- (UIColor *)randomColor\n{\n    CGFloat hue = (arc4random() % 256 / 256.0);\n    CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5;\n    CGFloat brightness = (arc4random() % 128 / 256.0 ) + 0.5;\n    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];\n}\n\u003c/pre\u003e\n\nAnd \u003ci\u003eUIImage\u003c/i\u003e from \u003ci\u003eUIColor\u003c/i\u003e:\n\n\u003cpre\u003e\n- (UIImage *)imageFromColor:(UIColor *)color\n{\n    CGRect rect = CGRectMake(0, 0, 1024, 1024);\n    UIGraphicsBeginImageContext(rect.size);\n    CGContextRef context = UIGraphicsGetCurrentContext();\n    CGContextSetFillColorWithColor(context, [color CGColor]);\n    CGContextFillRect(context, rect);\n    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();\n    UIGraphicsEndImageContext();\n    return image;\n}\n\u003c/pre\u003e\n\nAnd now we can render random images to \u003ci\u003ePDF\u003c/i\u003e file.\n\n\u003cpre\u003e\nUIGraphicsBeginPDFContextToFile(filename, CGRectMake(0, 0, 1024, 1024), nil);\n\nfor (NSInteger i = 0; i \u003c 1000; ++i) {\n    UIImage *image = [self imageFromColor:[self randomColor]];\n\n    @autoreleasepool {\n        UIGraphicsBeginPDFPage();\n        [image drawAtPoint:CGPointZero];\n    }\n}\n\nUIGraphicsEndPDFContext();\n\u003c/pre\u003e\n\n\u003cb\u003eAttention!\u003c/b\u003e Necessarily use \u003ci\u003e@autoreleasepool\u003c/i\u003e, otherwise, you will have memory leaks.\n\nAlso, I would like to provide some sample for generating \u003ci\u003ePDF\u003c/i\u003e files and merging these files. It’s also simple.\n\n\u003cpre\u003e\nNSMutableArray *pdfURLs = [NSMutableArray array];\n\nfor (NSInteger i = 0; i \u003c 1000; ++i) {\n    NSString *pdfFile = [NSString stringWithFormat:@”%@_%@”, filename, @(i)];\n    UIImage *imageToRender = [self imageFromColor:[self randomColor]];\n  \n    @autoreleasepool {\n        UIGraphicsBeginPDFContextToFile(pdfFile, CGRectMake(0, 0, 1024, 1024), nil);\n        UIGraphicsBeginPDFPage();\n        [imageToRender drawAtPoint:CGPointZero];\n        UIGraphicsEndPDFContext();\n    }\n\n    [pdfURLs addObject:[NSURL fileURLWithPath:pdfFile]];\n}\n\n[self combinePDFURLs:pdfURLs writeToURL:[NSURL fileURLWithPath:filename]];\n\nfor (NSURL *pdfUrl in pdfURLs) {\n    [[NSFileManager defaultManager] removeItemAtURL:pdfUrl error:nil];\n}\n\u003c/pre\u003e\n\nAnd of course, implementation of \u003ci\u003ecombinePDFURLs\u003c/i\u003e method, see below:\n\n\u003cpre\u003e\n- (void)combinePDFURLs:(NSArray *)PDFURLs writeToURL:(NSURL *)URL\n{\n    CGContextRef context = CGPDFContextCreateWithURL((__bridge CFURLRef)URL, NULL, NULL);\n\n    for (NSURL *PDFURL in PDFURLs) {\n        CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)PDFURL); \n        size_t numberOfPages = CGPDFDocumentGetNumberOfPages(document);\n\n        for (size_t pageNumber = 1; pageNumber \u003c= numberOfPages; ++pageNumber) {\n            CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber);\n            CGRect mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);\n            CGContextBeginPage(context, \u0026mediaBox);\n            CGContextDrawPDFPage(context, page);\n            CGContextEndPage(context);\n        }\n\n        CGPDFDocumentRelease(document);\n    }\n\n    CGPDFContextClose(context);\n    CGContextRelease(context);\n}\n\u003c/pre\u003e\n\nAnd the result:\n\n![alt tag](https://raw.github.com/maximbilan/iOS-UIImage-render-to-PDF/master/img/img2.png)\n\nHappy coding!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fios-uiimage-render-to-pdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximbilan%2Fios-uiimage-render-to-pdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fios-uiimage-render-to-pdf/lists"}