2023-08-17 10:12:26 +02:00
|
|
|
import SwiftUI
|
2023-08-18 22:47:24 +02:00
|
|
|
import SFSafeSymbols
|
2023-08-17 10:12:26 +02:00
|
|
|
|
|
|
|
struct ContentView: View {
|
2023-08-18 22:47:24 +02:00
|
|
|
|
|
|
|
let info: CVInfo
|
|
|
|
|
|
|
|
let style: CVStyle
|
|
|
|
|
2023-08-17 10:12:26 +02:00
|
|
|
var body: some View {
|
2023-08-18 22:47:24 +02:00
|
|
|
VStack(alignment: .leading) {
|
|
|
|
HStack {
|
|
|
|
Button(action: createAndSavePDF) {
|
|
|
|
Label("Save", systemSymbol: .squareAndArrowUp)
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
ScrollView(.vertical) {
|
|
|
|
CV(info: info, style: style)
|
|
|
|
}.frame(width: style.pageWidth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createAndSavePDF() {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard let pdfURL = renderPDF() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let url = showSavePanel() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writePDF(at: pdfURL, to: url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func showSavePanel() -> URL? {
|
|
|
|
let savePanel = NSSavePanel()
|
|
|
|
savePanel.allowedContentTypes = [.pdf]
|
|
|
|
savePanel.canCreateDirectories = true
|
|
|
|
savePanel.isExtensionHidden = false
|
|
|
|
savePanel.allowsOtherFileTypes = false
|
|
|
|
savePanel.title = "Save PDF"
|
|
|
|
savePanel.message = "Choose a location to save a PDF of the resume"
|
|
|
|
savePanel.nameFieldLabel = "File name:"
|
|
|
|
savePanel.nameFieldStringValue = "CV.pdf"
|
|
|
|
|
|
|
|
let response = savePanel.runModal()
|
|
|
|
guard response == .OK else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return savePanel.url
|
|
|
|
}
|
|
|
|
|
|
|
|
private func writePDF(at source: URL, to destination: URL) {
|
|
|
|
do {
|
|
|
|
if FileManager.default.fileExists(atPath: destination.path) {
|
|
|
|
try FileManager.default.removeItem(at: destination)
|
|
|
|
}
|
|
|
|
try FileManager.default.copyItem(at: source, to: destination)
|
|
|
|
} catch {
|
|
|
|
print("Failed to save pdf: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var content: some View {
|
|
|
|
CV(info: info, style: style)
|
|
|
|
.frame(width: style.pageWidth, height: style.pageHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private func renderPDF() -> URL? {
|
|
|
|
let pdfURL = URL.documentsDirectory.appending(path: "cv.pdf")
|
|
|
|
let renderer = ImageRenderer(content: content)
|
|
|
|
|
|
|
|
var didFinish = false
|
|
|
|
renderer.render { size, context in
|
|
|
|
var box = CGRect(x: 0, y: 0, width: size.width, height: size.height)
|
|
|
|
|
|
|
|
guard let pdf = CGContext(pdfURL as CFURL, mediaBox: &box, nil) else {
|
|
|
|
print("Failed to create CGContext")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let options: [CFString: Any] = [
|
|
|
|
kCGPDFContextMediaBox: CGRect(origin: .zero, size: size)
|
|
|
|
]
|
|
|
|
|
|
|
|
pdf.beginPDFPage(options as CFDictionary)
|
|
|
|
context(pdf)
|
|
|
|
pdf.endPDFPage()
|
|
|
|
pdf.closePDF()
|
|
|
|
didFinish = true
|
|
|
|
}
|
|
|
|
guard didFinish else {
|
|
|
|
return nil
|
2023-08-17 10:12:26 +02:00
|
|
|
}
|
2023-08-18 22:47:24 +02:00
|
|
|
print("PDF created")
|
|
|
|
return pdfURL
|
2023-08-17 10:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2023-08-18 22:47:24 +02:00
|
|
|
ContentView(info: cvInfo, style: cvStyle)
|
|
|
|
.frame(width: 600, height: 600 * sqrt(2))
|
2023-08-17 10:12:26 +02:00
|
|
|
}
|
|
|
|
}
|