How to create a PDF with Swift. I was able to create a PDF easily with PDFKit, which is nice.
Reference: Creating a PDF in Swift with PDFKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PDFKit | |
class PDFViewerViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let pdfData = createPDFData() | |
let pdfDocument = PDFDocument(data: pdfData) | |
let pdfView = PDFView() | |
pdfView.frame = view.bounds | |
pdfView.document = pdfDocument | |
view.addSubview(pdfView) | |
} | |
private func createPDFData() -> Data { | |
let pdfMetaData = [ | |
kCGPDFContextCreator: "@takoikatakotako", | |
kCGPDFContextAuthor: "swiswiswift.com" | |
] | |
let format = UIGraphicsPDFRendererFormat() | |
format.documentInfo = pdfMetaData as [String: Any] | |
// A4 Aspect | |
let pdfWidth: CGFloat = 2100 | |
let pdfHeight: CGFloat = 2900 | |
let pageRect = CGRect(x: 0, y: 0, width: pdfWidth, height: pdfHeight) | |
let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format) | |
let data = renderer.pdfData { (context) in | |
for i in 0..<5 { | |
context.beginPage() | |
// WriteText | |
let text = "Page \(i)" | |
let attributes = [ | |
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 72) | |
] | |
text.draw(at: CGPoint(x: 0, y: 0), withAttributes: attributes) | |
// WritePath | |
for _ in 0...10 { | |
let initialX = CGFloat.random(in: 0 ... pdfWidth) | |
let initialY = CGFloat.random(in: 0 ... pdfHeight) | |
let finalX = CGFloat.random(in: 0 ... pdfWidth) | |
let finalY = CGFloat.random(in: 0 ... pdfHeight) | |
let linePath = UIBezierPath() | |
linePath.move(to: CGPoint(x: initialX, y: initialY)) | |
linePath.addLine(to: CGPoint(x: finalX, y: finalY)) | |
let layer = CAShapeLayer() | |
layer.path = linePath.cgPath | |
layer.fillColor = UIColor.black.cgColor | |
layer.strokeColor = UIColor.black.cgColor | |
layer.lineWidth = 4 | |
layer.render(in: context.cgContext) | |
} | |
} | |
} | |
return data | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let button = UIButton() | |
button.frame.size = CGSize(width: 100, height: 100) | |
button.center = view.center | |
button.backgroundColor = .red | |
button.addTarget(self, action: #selector(tapped), for: .touchUpInside) | |
button.setTitle("Tap Me", for: .normal) | |
view.addSubview(button) | |
} | |
@objc func tapped() { | |
let pdfViewerViewController = PDFViewerViewController() | |
let navigationViewController = UINavigationController(rootViewController: pdfViewerViewController) | |
present(navigationViewController, animated: true, completion: nil) | |
} | |
} |