|
| 1 | +// |
| 2 | +// CarMaintenancePDFGenerator.swift |
| 3 | +// Basic-Car-Maintenance |
| 4 | +// |
| 5 | +// https://github.com/mikaelacaron/Basic-Car-Maintenance |
| 6 | +// See LICENSE for license information. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import PDFKit |
| 11 | + |
| 12 | +final class CarMaintenancePDFGenerator { |
| 13 | + private let vehicleName: String |
| 14 | + private let events: [MaintenanceEvent] |
| 15 | + |
| 16 | + // Define page margins |
| 17 | + private let topMargin: CGFloat = 50 |
| 18 | + private let bottomMargin: CGFloat = 50 |
| 19 | + private let leftMargin: CGFloat = 20 |
| 20 | + private let rightMargin: CGFloat = 20 |
| 21 | + private let columnWidth: CGFloat |
| 22 | + private let documentsDirectory = FileManager |
| 23 | + .default |
| 24 | + .urls(for: .documentDirectory, in: .userDomainMask) |
| 25 | + .first |
| 26 | + |
| 27 | + init(vehicleName: String, events: [MaintenanceEvent]) { |
| 28 | + self.vehicleName = vehicleName |
| 29 | + self.events = events |
| 30 | + self.columnWidth = (PageDimension.A4.pageWidth - leftMargin - rightMargin) / 3 |
| 31 | + } |
| 32 | + |
| 33 | + func generatePDF() -> PDFDocument? { |
| 34 | + guard !events.isEmpty else { return nil } |
| 35 | + let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(origin: .zero, size: PageDimension.A4.size)) |
| 36 | + let pdfData = pdfRenderer.pdfData { context in |
| 37 | + var yPosition: CGFloat = topMargin |
| 38 | + |
| 39 | + beginNewPage( |
| 40 | + context: context, |
| 41 | + yPosition: &yPosition, |
| 42 | + isFirstPage: true |
| 43 | + ) |
| 44 | + |
| 45 | + let tableRowAttributes: [NSAttributedString.Key: Any] = [ |
| 46 | + .font: UIFont.systemFont(ofSize: 14), |
| 47 | + .foregroundColor: UIColor.black |
| 48 | + ] |
| 49 | + |
| 50 | + for (index, event) in events.enumerated() { |
| 51 | + if yPosition + 60 > PageDimension.A4.pageHeight - bottomMargin { |
| 52 | + beginNewPage( |
| 53 | + context: context, |
| 54 | + yPosition: &yPosition, |
| 55 | + isFirstPage: index == 0 |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + event.date |
| 60 | + .formatted() |
| 61 | + .draw(at: CGPoint(x: leftMargin, y: yPosition), withAttributes: tableRowAttributes) |
| 62 | + |
| 63 | + vehicleName.draw( |
| 64 | + at: CGPoint(x: leftMargin + columnWidth, y: yPosition), |
| 65 | + withAttributes: tableRowAttributes |
| 66 | + ) |
| 67 | + |
| 68 | + let notesRect = CGRect( |
| 69 | + x: leftMargin + 2 * columnWidth, |
| 70 | + y: yPosition, |
| 71 | + width: columnWidth - 20, |
| 72 | + height: 50 |
| 73 | + ) |
| 74 | + event.notes.draw(in: notesRect, withAttributes: tableRowAttributes) |
| 75 | + |
| 76 | + yPosition += 60 |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + do { |
| 81 | + guard let fileURL = documentsDirectory? |
| 82 | + .appendingPathComponent("\(vehicleName)-MaintenanceReport.pdf") |
| 83 | + else { return nil } |
| 84 | + |
| 85 | + if FileManager.default.fileExists(atPath: fileURL.absoluteString) { |
| 86 | + try FileManager.default.removeItem(at: fileURL) |
| 87 | + } |
| 88 | + |
| 89 | + try pdfData.write(to: fileURL) |
| 90 | + print("PDF saved to: \(fileURL.path)") |
| 91 | + return PDFDocument(url: fileURL) |
| 92 | + } catch { |
| 93 | + print("Could not save the PDF: \(error)") |
| 94 | + return nil |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Draw the center header and header columns |
| 99 | + private func drawHeader(context: UIGraphicsPDFRendererContext, yPosition: inout CGFloat) { |
| 100 | + let titleAttributes: [NSAttributedString.Key: Any] = [ |
| 101 | + .font: UIFont.boldSystemFont(ofSize: 20), |
| 102 | + .foregroundColor: UIColor.black |
| 103 | + ] |
| 104 | + let subtitleAttributes: [NSAttributedString.Key: Any] = [ |
| 105 | + .font: UIFont.systemFont(ofSize: 16), |
| 106 | + .foregroundColor: UIColor.black |
| 107 | + ] |
| 108 | + |
| 109 | + let titleString = "Basic Car Maintenance" |
| 110 | + let eventTitleString = "Maintenance Events" |
| 111 | + guard |
| 112 | + let startDate = events.first?.date.formatted(date: .numeric, time: .omitted), |
| 113 | + let endDate = events.last?.date.formatted(date: .numeric, time: .omitted) |
| 114 | + else { return } |
| 115 | + let dateRangeString = "From \(startDate) to \(endDate)" |
| 116 | + |
| 117 | + let titleSize = titleString.size(withAttributes: titleAttributes) |
| 118 | + let vehicleSize = vehicleName.size(withAttributes: subtitleAttributes) |
| 119 | + let eventTitleSize = eventTitleString.size(withAttributes: subtitleAttributes) |
| 120 | + let dateRangeSize = dateRangeString.size(withAttributes: subtitleAttributes) |
| 121 | + |
| 122 | + let titleX = (PageDimension.A4.pageWidth - titleSize.width) / 2 |
| 123 | + let vehicleX = (PageDimension.A4.pageWidth - vehicleSize.width) / 2 |
| 124 | + let eventTitleX = (PageDimension.A4.pageWidth - eventTitleSize.width) / 2 |
| 125 | + let dateRangeX = (PageDimension.A4.pageWidth - dateRangeSize.width) / 2 |
| 126 | + |
| 127 | + titleString.draw(at: CGPoint(x: titleX, y: yPosition), withAttributes: titleAttributes) |
| 128 | + yPosition += 30 |
| 129 | + |
| 130 | + vehicleName.draw(at: CGPoint(x: vehicleX, y: yPosition), withAttributes: subtitleAttributes) |
| 131 | + yPosition += 30 |
| 132 | + |
| 133 | + eventTitleString.draw(at: CGPoint(x: eventTitleX, y: yPosition), withAttributes: subtitleAttributes) |
| 134 | + yPosition += 30 |
| 135 | + |
| 136 | + dateRangeString.draw(at: CGPoint(x: dateRangeX, y: yPosition), withAttributes: subtitleAttributes) |
| 137 | + yPosition += 50 |
| 138 | + |
| 139 | + drawColumnsHeaders(yPosition: &yPosition) |
| 140 | + } |
| 141 | + |
| 142 | + private func beginNewPage( |
| 143 | + context: UIGraphicsPDFRendererContext, |
| 144 | + yPosition: inout CGFloat, |
| 145 | + isFirstPage: Bool |
| 146 | + ) { |
| 147 | + context.beginPage() |
| 148 | + yPosition = topMargin |
| 149 | + |
| 150 | + if isFirstPage { |
| 151 | + drawHeader(context: context, yPosition: &yPosition) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private func drawColumnsHeaders(yPosition: inout CGFloat) { |
| 156 | + let dateColumnHeader = "Date" |
| 157 | + let vehicleColumnHeader = "Vehicle Name" |
| 158 | + let noteColumnHeader = "Notes" |
| 159 | + |
| 160 | + let subtitleAttributes: [NSAttributedString.Key: Any] = [ |
| 161 | + .font: UIFont.boldSystemFont(ofSize: 16), |
| 162 | + .foregroundColor: UIColor.black |
| 163 | + ] |
| 164 | + |
| 165 | + dateColumnHeader.draw( |
| 166 | + at: CGPoint(x: leftMargin, y: yPosition), |
| 167 | + withAttributes: subtitleAttributes |
| 168 | + ) |
| 169 | + |
| 170 | + vehicleColumnHeader.draw( |
| 171 | + at: CGPoint(x: leftMargin + columnWidth, y: yPosition), |
| 172 | + withAttributes: subtitleAttributes |
| 173 | + ) |
| 174 | + |
| 175 | + let notesRect = CGRect( |
| 176 | + x: leftMargin + 2 * columnWidth, |
| 177 | + y: yPosition, |
| 178 | + width: columnWidth - 20, |
| 179 | + height: 50 |
| 180 | + ) |
| 181 | + noteColumnHeader.draw(in: notesRect, withAttributes: subtitleAttributes) |
| 182 | + |
| 183 | + yPosition += 30 |
| 184 | + } |
| 185 | +} |
0 commit comments