1. Products
  2.   PDF
  3.   Swift
  4.   SwiftyHaru
 
  

Open Source Swift PDF Generation & Manipulation API

Free Swift PDF Files Processing API that enables Software Developers to Generate, Edit, View, Manipulate and Render PDF Documents.

SwiftyHaru is a very feature-rich and powerful, open source PDF generation library for Swift that offers software developers the ability to create and process PDF documents with ease. The library is based on the Haru Free PDF Library, which is a cross-platform C library for generating PDF documents. It provides a Swift wrapper around the Haru Free PDF Library, making it easy for Swift developers to generate PDF documents. SwiftyHaru is an open source library, which means that developers can modify and customize the library to suit their specific needs.

The SwiftyHaru library is designed to be easy to use and offers a range of features for creating high-quality PDF documents. It includes support for a wide range of PDF manipulation features, such as rating PDF documents in real-time, adding content to the PDF document, font embedding, image embedding, saving the PDF document to disk, and vector graphics. The library is also optimized for performance, making it ideal for generating PDF documents in real time.

SwiftyHaru is an excellent choice for Swift developers who want to create high-quality PDF documents inside their own Swift applications. The library is very stable and brings the safety of Swift to the process of creating PDFs on different platforms like Linux, macOS, iOS, watchOS, and tvOS. Whether you need to generate reports, invoices, or other types of documents, SwiftyHaru is an excellent choice for Swift developers.

Previous Next

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.

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.

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")

 English