0

I am creating a PDF and sharing it via UIActivityViewController.

func createFlyer() -> Data {
  // 1
  let pdfMetaData = [
    kCGPDFContextCreator: "Flyer",
  ]
  let format = UIGraphicsPDFRendererFormat() 
  format.documentInfo = pdfMetaData as [String: Any]

  // 2
  let pageWidth = 8.5 * 72.0
  let pageHeight = 11 * 72.0
  let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

  // 3
  let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
  // 4
  let data = renderer.pdfData { (context) in
    // 5
    context.beginPage()
    // 6
    let attributes = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 72)
    ]
    let text = "I'm a PDF!"
    text.draw(at: CGPoint(x: 0, y: 0), withAttributes: attributes)
  }

  return data
}

func sharePDF () {
    let activityViewController = UIActivityViewController(
        activityItems: [data],  applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivity.ActivityType.saveToCameraRoll]

    if let popoverPresentationController = activityViewController.popoverPresentationController {
        popoverPresentationController.barButtonItem = self.sharePDFButton
    }
    self.present(activityViewController, animated: true, completion: {
    })   
}

Everything is fine, except for the file name is generated by the system ('PDF Document.pdf').

Is there a way to have a custom file name?

2
  • 2
    Haven't tested this and there may be a better way of accomplishing this, but you could try saving the file to a temporary folder first. Like so: stackoverflow.com/a/52467491/12555191 Commented Dec 30, 2019 at 17:04
  • Not the most convenient way, but it seems to work. Commented Dec 30, 2019 at 17:38

1 Answer 1

2

Please try to use below code while presenting activityViewController, I hope it works.

let fileManager = FileManager.default
let documentoPath = getDirectoryPath().appendingPathComponent(url.lastPathComponent) as URL
print("documentoPath :\(documentoPath)")
if  fileManager.fileExists(atPath: documentoPath.path) {
     DispatchQueue.main.async {
        let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documentoPath], applicationActivities: nil)
                            activityViewController.popoverPresentationController?.sourceView = self.view
                           self.downloadNavController?.present(activityViewController, animated: true, completion: { () in
      UIBarButtonItem.appearance().tintColor = UIColor.systemBlue
      UINavigationBar.appearance().barTintColor = UIColor.white
          })
       }
 }

You can find getDirectoryPath() here:

func getDirectoryPath() -> URL {
    var nestedFolderURL: URL?
    do{
        let rootFolderURL = try FileManager.default.url(
            for: .documentDirectory,
            in: .userDomainMask,
            appropriateFor: nil,
            create: false
        )
        nestedFolderURL = rootFolderURL
        
        if !FileManager.default.fileExists(atPath: nestedFolderURL!.relativePath) {
            try FileManager.default.createDirectory(
                at: nestedFolderURL!,
                withIntermediateDirectories: false,
                attributes: nil
            )
        }
        return nestedFolderURL!
    } catch (let error) {
        print(error)
    }
    
    return nestedFolderURL!
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.