{"id":1547,"url":"https://github.com/sgr-ksmt/PDFGenerator","last_synced_at":"2025-08-06T14:31:30.611Z","repository":{"id":44454162,"uuid":"51076118","full_name":"sgr-ksmt/PDFGenerator","owner":"sgr-ksmt","description":"A simple generator of PDF written in Swift. ","archived":false,"fork":false,"pushed_at":"2020-10-27T14:30:02.000Z","size":939,"stargazers_count":757,"open_issues_count":50,"forks_count":110,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-10-13T14:43:46.554Z","etag":null,"topics":["apple-swift","carthage","cocoapods","generator","ios","pdf","swift","swift-library","tool"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/sgr-ksmt.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"sgr-ksmt"}},"created_at":"2016-02-04T13:00:50.000Z","updated_at":"2024-09-17T14:41:50.000Z","dependencies_parsed_at":"2022-08-12T11:11:14.023Z","dependency_job_id":null,"html_url":"https://github.com/sgr-ksmt/PDFGenerator","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgr-ksmt%2FPDFGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgr-ksmt%2FPDFGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgr-ksmt%2FPDFGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgr-ksmt%2FPDFGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sgr-ksmt","download_url":"https://codeload.github.com/sgr-ksmt/PDFGenerator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228830729,"owners_count":17978342,"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":["apple-swift","carthage","cocoapods","generator","ios","pdf","swift","swift-library","tool"],"created_at":"2024-01-05T20:15:49.519Z","updated_at":"2024-12-09T14:31:18.446Z","avatar_url":"https://github.com/sgr-ksmt.png","language":"Swift","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"#features\"\u003eFeatures\u003c/a\u003e |\n  \u003ca href=\"#requirements\"\u003eRequirements\u003c/a\u003e |\n  \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e |\n  \u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e |\n  \u003ca href=\"#communication\"\u003eCommunication\u003c/a\u003e |\n  \u003ca href=\"#license\"\u003eLICENSE\u003c/a\u003e\n\u003c/p\u003e\n\n# PDFGenerator\n[![Build Status](https://travis-ci.org/sgr-ksmt/PDFGenerator.svg?branch=master)](https://travis-ci.org/sgr-ksmt/PDFGenerator)\n[![GitHub release](https://img.shields.io/github/release/sgr-ksmt/PDFGenerator.svg)](https://github.com/sgr-ksmt/PDFGenerator/releases)\n[![codecov](https://codecov.io/gh/sgr-ksmt/PDFGenerator/branch/master/graph/badge.svg)](https://codecov.io/gh/sgr-ksmt/PDFGenerator)\n[![Language](https://img.shields.io/badge/language-Swift%204.2-orange.svg)]()\n[![Carthage](https://img.shields.io/badge/Carthage-✓-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods](https://img.shields.io/badge/Cocoa%20Pods-✓-4BC51D.svg?style=flat)](https://cocoapods.org/pods/PDFGenerator)\n[![CocoaPodsDL](https://img.shields.io/cocoapods/dt/PDFGenerator.svg)]()\n[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/matteocrippa/awesome-swift#pdf)\n[![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)\n\u003cbr /\u003e\n\n`PDFGenerator` is a simple PDF generator that generates with `UIView`, `UIImage`, ...etc .\n\n```swift\ndo {\n    let page: [PDFPage] = [\n        .whitePage(CGSize(width: 200.0, height: 100.0)),\n        .image(image1)\n        .image(image2)\n        .imagePath(lastPageImagePath)\n        .whitePage(CGSize(width: 200.0, height: 100.0))\n    ]\n    let path = NSTemporaryDirectory().appending(\"sample1.pdf\")\n    try PDFGenerator.generate(page, to: path, password: \"123456\")\n} catch let error {\n    print(error)\n}\n```\n\n## Features\n- **Swift 5 is ready** :pray:\n- Multiple pages support.\n- Also generate PDF with `image path`, `image binary`, `image ref (CGImage)`\n- Good memory management.\n- UIScrollView support : If view is `UIScrollView`, `UITableView`, `UICollectionView`, `UIWebView`, drawn whole content.\n- Outputs as binary(`Data`) or writes to Disk(in given file path) directly.\n- Corresponding to Error-Handling. Strange PDF has never been generated!!.\n- DPI support. : Default dpi is 72.\n- Password protection support.\n\n## Requirements\n- iOS 9.0+\n- Xcode 11+\n- Swift 5.1\n\n## Installation\n\n### Carthage\n\n- Add the following to your *Cartfile*:\n\n```bash\ngithub \"sgr-ksmt/PDFGenerator\" ~\u003e 3.1\n```\n\n- Then run command:\n\n```bash\n$ carthage update\n```\n\n- Add the framework as described.\n\u003cbr\u003e Details: [Carthage Readme](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application)\n\n\n#### CocoaPods\n\n**PDFGenerator** is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'PDFGenerator', '~\u003e 3.1'\n```\n\nand run `pod install`\n\n## Usage\n\n### Generate from view(s) or image(s)\n- UIView → PDF\n\n```swift\nfunc generatePDF() {\n    let v1 = UIScrollView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))\n    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))\n    let v3 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))\n    v1.backgroundColor = .red\n    v1.contentSize = CGSize(width: 100.0, height: 200.0)\n    v2.backgroundColor = .green\n    v3.backgroundColor = .blue\n\n    let dst = URL(fileURLWithPath: NSTemporaryDirectory().appending(\"sample1.pdf\"))\n    // outputs as Data\n    do {\n        let data = try PDFGenerator.generated(by: [v1, v2, v3])\n        try data.write(to: dst, options: .atomic)\n    } catch (let error) {\n        print(error)\n    }\n\n    // writes to Disk directly.\n    do {\n        try PDFGenerator.generate([v1, v2, v3], to: dst)    \n    } catch (let error) {\n        print(error)\n    }\n}\n```\n\n`Also PDF can generate from image(s), image path(s) same as example.`\n\n### Generate from PDFPage object\n\n- (UIVIew or UIImage) → PDF\n\nUse `PDFPage`.\n\n```swift\npublic enum PDFPage {\n    case whitePage(CGSize) // = A white view\n    case view(UIView)\n    case image(UIImage)\n    case imagePath(String)\n    case binary(Data)\n    case imageRef(CGImage)\n}\n```\n\n```swift\nfunc generatePDF() {\n    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))\n    v1.backgroundColor = .red\n    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))\n    v2.backgroundColor = .green\n\n    let page1 = PDFPage.View(v1)\n    let page2 = PDFPage.View(v2)\n    let page3 = PDFPage.WhitePage(CGSizeMake(200, 100))\n    let page4 = PDFPage.Image(UIImage(contentsOfFile: \"path/to/image1.png\")!)\n    let page5 = PDFPage.ImagePath(\"path/to/image2.png\")\n    let pages = [page1, page2, page3, page4, page5]\n\n    let dst = NSTemporaryDirectory().appending(\"sample1.pdf\")\n    do {\n        try PDFGenerator.generate(pages, to: dst)\n    } catch (let e) {\n        print(e)\n    }\n}\n```\n\n### Generate custom dpi PDF\n```swift\n// generate dpi300 PDF (default: 72dpi)\nfunc generatePDF() {\n    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))\n    v1.backgroundColor = .red\n    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))\n    v2.backgroundColor = .green\n\n    let page1 = PDFPage.View(v1)\n    let page2 = PDFPage.View(v2)\n    let pages = [page1, page2]\n\n    let dst = NSTemporaryDirectory().appending(\"sample1.pdf\")\n    do {\n        try PDFGenerator.generate(pages, to: dst, dpi: .dpi_300)\n    } catch (let e) {\n        print(e)\n    }\n}\n```\n\n### Password protection\n```swift\n// generate PDF with password: 123456\nfunc generatePDF() {\n    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))\n    v1.backgroundColor = .red\n    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))\n    v2.backgroundColor = .green\n\n    let page1 = PDFPage.view(v1)\n    let page2 = PDFPage.view(v2)\n    let pages = [page1, page2]\n\n    let dst = NSTemporaryDirectory().appending(\"sample1.pdf\")\n    do {\n        try PDFGenerator.generate(pages, to: dst, password: \"123456\")\n        // or use PDFPassword model\n        try PDFGenerator.generate(pages, to: dst, password: PDFPassword(\"123456\"))\n        // or use PDFPassword model and set user/owner password\n        try PDFGenerator.generate(pages, to: dst, password: PDFPassword(user: \"123456\", owner: \"abcdef\"))\n    } catch let error {\n        print(error)\n    }\n}\n```\n\n## Communication\n- If you found a bug, please open an issue. :bow:\n- Also, if you have a feature request, please open an issue. :thumbsup:\n- If you want to contribute, submit a pull request.:muscle:\n\n## License\n\n**PDFGenerator** is under MIT license. See the [LICENSE](LICENSE) file for more info.\n","funding_links":["https://github.com/sponsors/sgr-ksmt"],"categories":["Media","Libs","SWIFT","Libraries","PDF [🔝](#readme)"],"sub_categories":["PDF","Other free courses","Utility","Swift"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgr-ksmt%2FPDFGenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsgr-ksmt%2FPDFGenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgr-ksmt%2FPDFGenerator/lists"}