SPB Git

Repository Public License

make-sample-pdfs.swift 6.6 KB 138 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX OS Automation9// File             : Workflows/Samples/pdf-to-structured-report/make-sample-pdfs.swift10// Module           : Workflows11// Repository       : https://git.spboucher.ai/orix-automation12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788// ORIX OS Automation89// File: Workflows/Samples/pdf-to-structured-report/make-sample-pdfs.swift90// Description: Regenerates the synthetic sample PDFs of the "PDF to structured data and report" example with CoreGraphics and CoreText.91//92// Author: Simon-Pierre Boucher93// Contact: contact@spboucher.ai94//95// Usage: swift make-sample-pdfs.swift <output folder>      (the committed files were made with: swift make-sample-pdfs.swift Input)96import CoreGraphics97import CoreText98import Foundation99100let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : "Input")101try FileManager.default.createDirectory(at: out.appendingPathComponent("archive"), withIntermediateDirectories: true)102103/// Writes one PDF: each element of `pages` is the text of one page; nil draws shapes only (a page without a text layer).104func makePDF(_ name: String, title: String?, pages: [String?]) {105    var box = CGRect(x: 0, y: 0, width: 612, height: 792)106    var info: [CFString: Any] = [kCGPDFContextCreator: "ORIX sample generator"]107    if let title { info[kCGPDFContextTitle] = title; info[kCGPDFContextAuthor] = "ORIX samples" }108    guard let ctx = CGContext(out.appendingPathComponent(name) as CFURL, mediaBox: &box, info as CFDictionary) else { fatalError("cannot create \(name)") }109    for text in pages {110        ctx.beginPDFPage(nil)111        if let text {112            let font = CTFontCreateWithName("Helvetica" as CFString, 13, nil)113            let attributed = NSAttributedString(string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font])114            let frame = CTFramesetterCreateFrame(CTFramesetterCreateWithAttributedString(attributed), CFRange(location: 0, length: 0),115                                                 CGPath(rect: box.insetBy(dx: 72, dy: 72), transform: nil), nil)116            CTFrameDraw(frame, ctx)117        } else {118            ctx.setFillColor(CGColor(gray: 0.85, alpha: 1)); ctx.fill(CGRect(x: 72, y: 500, width: 468, height: 200))119            ctx.setStrokeColor(CGColor(gray: 0.3, alpha: 1)); ctx.setLineWidth(3)120            for i in 0..<6 { ctx.stroke(CGRect(x: 90 + Double(i) * 70, y: 200, width: 50, height: 180)) }121        }122        ctx.endPDFPage()123    }124    ctx.closePDF()125    print("wrote \(name)")126}127128makePDF("quarterly-summary.pdf", title: "Quarterly summary Q3", pages: [129    "Quarterly summary — third quarter (synthetic sample)\n\nRevenue grew by four percent. Support tickets fell by a tenth.\nThe team shipped the CSV export and postponed the PDF export.",130])131makePDF("field-notes.pdf", title: "Field notes", pages: [132    "Field notes, page one (synthetic sample)\n\nSite A: two battery containers, both online.\nSite B: one inverter replaced in October.",133    "Field notes, page two\n\nOpen questions: spare inverter contract, recycling rates.",134    "Field notes, page three\n\nNext visit planned for the first week of December.",135])136makePDF("untitled-memo.pdf", title: nil, pages: ["Memo without a title property (synthetic sample).\n\nPlease read before Friday."])137makePDF("archive/scan-without-text.pdf", title: "Scanned page", pages: [nil])138
Files main
Menu

Theme