Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give PDF Data a filename for user to save in Swift

I give my pdfData to user to save. He can save to files and make a file, but the default name of the pdf file is: PDF document.pdf. I want my own filename if this is possible. Perhaps I can change the filename within the pdfData before I give pdfData to UIActivityViewController?

Here is my code:

// Create page rect
let pageRect = CGRect(x: 0, y: 0, width: 595.28, height: 841.89) // A4, 72 dpi

// Create PDF context and draw
let pdfData = NSMutableData()

UIGraphicsBeginPDFContextToData(pdfData, pageRect, nil)
UIGraphicsBeginPDFPage()

// From here you can draw page, best make it in a function
PdfErstellung.PdfErstellen(auswahlZeilen, vitalstoffWerteListe, heuteString)

UIGraphicsEndPDFContext()

// Save pdf DATA through user
let activityViewController = UIActivityViewController(activityItems: [pdfData], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // für IPAD nötig
self.present(activityViewController, animated: true, completion: nil)
   

-- UPDATE --

My new idea ist, first try save the file and try a URL, and if this fail, then use the pdfData directly, because in some simulator use URL give no error and in other give error.

More here: https://stackoverflow.com/a/52499637/10392572

like image 635
Lukas Hediner Avatar asked Sep 23 '18 15:09

Lukas Hediner


2 Answers

You just need to save your pdfData to a temporary fileURL and share that URL.

let temporaryFolder = FileManager.default.temporaryDirectory
let fileName = "document.pdf"
let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
print(temporaryFileURL.path)  // /Users/lsd/Library/Developer/XCPGDevices/E2003834-07AB-4833-B206-843DC0A52967/data/Containers/Data/Application/322D1F1D-4C97-474C-9040-FE5E740D38CF/tmp/document.pdf
do {
    try pdfData.write(to: temporaryFileURL)
    // your code
    let activityViewController = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil)
} catch {
    print(error)
}
like image 50
Leo Dabus Avatar answered Oct 19 '22 03:10

Leo Dabus


Note that you can also set a user title for AirDrop to be used as follows:

    let temporaryFolder = FileManager.default.temporaryDirectory
    let fileName = "Carburant Discount Historique des Prix au \(Date()).json"
    let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
    print(temporaryFileURL.path)
    do {
        try? FileManager.default.removeItem(at: temporaryFileURL)

        try json.write(to: temporaryFileURL)
    }
    catch let error {
        print("\(#function): *** Error while writing json to temporary file. \(error.localizedDescription)")
        alert("Export impossible")
        return
    }

    /// Sets the **title** along with the URL
    let dataToShare: [Any] = ["Historique des prix", temporaryFileURL]

    let activityViewController = UIActivityViewController(activityItems: dataToShare, applicationActivities: nil)
like image 37
Stéphane de Luca Avatar answered Oct 19 '22 03:10

Stéphane de Luca