Open Source Swift PDF Generation & Manipulation API
Free Swift PDF Files Processing API that enables Software Developers to Generate, Edit, View, Manipulate Manipulate Split/Merge, Manage Annotation and Render PDF Documents.
What is One SwiftyHaru?
SwiftyHaru is a robust and feature-rich Swift PDF library that empowers developers to create PDF files and manage complex document workflows with ease. Built as a Swift wrapper around the well-known Haru Free PDF Library—a cross-platform C library—SwiftyHaru brings the power and flexibility of Haru into the Swift ecosystem. This open source library is fully customizable, enabling developers to tailor PDF generation features to their specific project requirements. With support for macOS, iOS, Linux, watchOS, and tvOS, SwiftyHaru offers a reliable and cross-platform solution for developers looking to generate PDF files efficiently.
SwiftyHaru includes extensive functionality for generating high-quality PDF content, including real-time document rendering, text and image embedding, vector graphics, font embedding, and saving files to disk. Its performance-optimized design makes it suitable for real-time applications like reports, invoices, and dynamic forms. While the core focus is on document creation, SwiftyHaru can also be extended to handle tasks like Split/Merge PDFs and Manage PDF Annotation using additional tools or custom modules. With its intuitive API and stability across platforms, SwiftyHaru is a go-to choice for developers seeking a powerful and flexible Swift-based solution for all their PDF document needs.
Getting Started with SwiftyHaru
The recommend way to install SwiftyHaru is using CocoaPods. Please use the following command for a smooth installation.
Install SwiftyHaru via CocoaPods
Install SwiftyHaru via CocoaPods
use_frameworks!
pod 'SwiftyHaru'
You can download it directly from GitHub.
Create & Manage PDF File via Swift API
The open source SwiftyHaru library enables software developers to create new PDF documents with just a couple of lines of Swift code. There are also several important features part of the library for PDF document management, such as open existing documents, reading documents from the stream, handle pages inside PDF, specify the numbers of pages, manage page layout, set and get page mode, handle fonts, setting for page layout, PDF encodings support, enables Japanese encodings, insert images to PDF page, load image from a file, set permission (read, print, edit), and many more.
How to Create a Simple PDF Document via Swift API?
import SwiftyHaru
let pdf = PDFDocument()
let page = pdf.addPage()
let font = pdf.addFont("Helvetica-Bold")
let text = "Hello, World!"
let fontSize: Float = 24
page.beginText()
page.setFontAndSize(font, fontSize)
page.textOut(100, 100, text)
page.endText()
pdf.write(toFile: "/path/to/document.pdf")
Embed Images in PDF via Swift Library
The open source library SwiftyHaru makes it easy for software developers to create and embed images in PDFs inside Swift applications. The library has included various important features for handling image-related tasks, such as getting the size of an existing image, getting the width of the image, getting the number of bits used to describe each color component, setting the transparent color of the image, and many more. The following example shows how to embed an image in a PDF document using Swift commands.
How to PDF Document with an Embedded Image via Swift API?
import SwiftyHaru
let pdf = PDFDocument()
let image = UIImage(named: "yourImageName")
// Create New PDF Page
let page = pdf.addPage()
// Get the size of the image:
let imageSize = image.size
// Calculate the width and height of the image in points:
let widthInPoints = (imageSize.width / image.scale) * 72.0
let heightInPoints = (imageSize.height / image.scale) * 72.0
// Add the image to the PDF page:
let x = 0.0 // X coordinate of the top-left corner of the image
let y = 0.0 // Y coordinate of the top-left corner of the image
let imageRect = CGRect(x: x, y: y, width: widthInPoints, height: heightInPoints)
page.addImage(image, rect: imageRect)
//Save PDF
let data = pdf.generatePDFdata()
// You can then save the data to a file or display it in a PDF viewer
Add & Manage PDF Annotation via Swift API
PDF annotation is an important part of a PDF document that allows users to add custom content on PDF pages such as extra text, graphics or objects etc. The open source library SwiftyHaru makes it easy for software developers to add different types of annotations to their PDF documents using Swift API. The library supports text annotation, Link annotation, setting the appearance of a text annotation, setting web-link annotation, defining the style of the annotation's icon, and many more. The following example demonstrates how to add text annotation to a PDF document using Swift commands.
How to Add Text Annotation to PDF Document via Swift API?
import SwiftyHaru
// Load an existing PDF document
let pdf = PDFDocument(fileAtPath: "example.pdf")!
// Get the first page of the PDF document
let page = pdf.getPage(1)
// Create a text annotation with a yellow background color
let annotation = PDFAnnotation(text: "Example Annotation", rect: CGRect(x: 100, y: 100, width: 200, height: 50))
annotation.color = .yellow
// Add the annotation to the PDF page
page.addAnnotation(annotation)
// Save the modified PDF document
pdf.write(toFile: "example-with-annotation.pdf")