【问题标题】:How to give PDF Data a filename for user to save in Swift如何为 PDF 数据提供文件名以供用户在 Swift 中保存
【发布时间】:2019-02-27 06:12:03
【问题描述】:

我将我的 pdfData 提供给用户保存。他可以保存到文件并制作文件,但pdf文件的默认名称是:PDF document.pdf。如果可能的话,我想要我自己的文件名。也许我可以在将 pdfData 提供给UIActivityViewController 之前更改 pdfData 中的文件名?

这是我的代码:

// 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)
   

-- 更新--

我的新想法是,先尝试保存文件并尝试URL,如果失败,则直接使用pdfData,因为在某些模拟器中使用URL不会出错,而在其他情况下会出错。

更多:https://stackoverflow.com/a/52499637/10392572

【问题讨论】:

    标签: swift pdf save filenames


    【解决方案1】:

    请注意,您还可以为 AirDrop 设置用户标题,如下所示:

        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)
    

    【讨论】:

    • 在我更改行之前出现错误:尝试 json.write... TO:尝试 json.write(to:temporaryFileURL, atomically: true, encoding: .utf8)
    • 不需要删除文件,只需使用原子写入选项即可
    【解决方案2】:

    您只需将 pdfData 保存到临时文件 URL 并共享该 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)
    }
    

    【讨论】:

    • 我知道,但是 url 不适合,因为它给了我这个错误:Vitalstoffcontroller[6161:165020] [default] [ERROR] Failed to determine if URL /Users/lukashedinger/Library/ Developer/CoreSimulator/Devices/4DE0F1A6-67B7-4909-8C46-78278D403E63/data/Containers/Data/Application/D72F4EE0-D330-4B2D-924E-E17FC805EF55/tmp/Vitalstoffwerte 21092018-21092018.pdf (n) 由文件管理提供者。我在这里问过这个问题:stackoverflow.com/q/52457064/10392572
    • 确保您传递的 url 对象不是路径字符串,如上所示
    • 请注意,一旦您的方法完成,临时文件将被删除
    • 我不想制作文件,因为作为数据,用户可以用我的数据打印和制作pdf,而不仅仅是保存它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2016-07-15
    • 2021-07-23
    • 2011-01-11
    相关资源
    最近更新 更多