0

I am successfully downloading a PDF from api end point. Once pdf is downloaded, the title of pdf is : PDF document.pdf . How to change the title of PDF?

I tried to update metadata of PDF using PDFDocumentAttribute (see below), but it is not working.

var metadata = pdfDocument.documentAttributes!
metadata[PDFDocumentAttribute.subjectAttribute] = "subject attribute"
metadata[PDFDocumentAttribute. titleAttribute] = "title attribute"
pdfDocument.documentAttributes = metadata

Note: I am not using FileManager

How I am fetching PDF:-

let task = session.dataTask(with: urlRequest) { (data, _, error) in
            DispatchQueue.main.async {
                guard let unwrappedData = data, error == nil else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                completion(.success(pdfDocument))
            }
        }
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • How did you download it? Isn't that the name of the file, as it's its path? – Larme May 19 '22 at 22:09
  • @Larme Thank you for your comment. I've updated question description and added code snippet related to downloading pdf. Please check. – pkc456 May 19 '22 at 22:12
  • could you show the code where and how you print the "title" of the pdf document, giving you: `PDF document.pdf`. – workingdog support Ukraine May 20 '22 at 01:21
  • I am setting the title of pdf file (see full code snippet at question description): `metadata[PDFDocumentAttribute. titleAttribute] = "title attribute"` I save this pdf file using `save to files` option (UIActivityController). While saving, the name of pdf file is `PDF Document.pdf` – pkc456 May 20 '22 at 02:42

1 Answers1

1

try this:

pdfDocument.documentAttributes?["Title"] = "my title attribute"

or

pdfDocument.documentAttributes?[PDFDocumentAttribute.titleAttribute] = "my title attribute"

Similarly for PDFDocumentAttribute.subjectAttribute.

The above will set the Title of your document, and when you save it, the file name will be whatever file name you give it.

EDIT-1: saving the pdfDocument to a file with a chosen file name.

       DispatchQueue.main.async {
            guard let unwrappedData = data, error == nil else {
                completion(.failure(error ?? Constants.dummyError))
                return
            }
            guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                completion(.failure(error ?? Constants.dummyError))
                return
            }
            
            // set the Title
            pdfDocument.documentAttributes?[PDFDocumentAttribute.titleAttribute] = "my title attribute"
            
            do {
                // save the document to the given file name ("mydoc.pdf")
                let docURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("mydoc.pdf")  // <-- here file name
                pdfDocument.write(to: docURL)
                print("\n docUrl: \(docURL.absoluteString)\n")
            }
            catch {
                print("Error \(error)")
            }
            completion(.success(pdfDocument))
        }
  • Thank you for your answer. I already tried to set the `title` attribute. It is not changing the file name – pkc456 May 20 '22 at 15:02
  • 1
    that's right, changing the `Title` does not change the file name. The `Title` and `File name` are completely different. They have no relation to each other. To `change` the file name, do it when you save the file (eg. a local file url). The file will be given the `file name` you set it, and the `pdfDocument` will still have the `Title` attribute you set it previously. I have tested this, and it works very well. Show the code you use to `save` your `pdfDocument` to a file, I can then advise you how to change the name. – workingdog support Ukraine May 20 '22 at 22:43
  • updated my answer with some example code to show how to change the `Title` attribute of the `pdfDocument`, and how to give it a `file name` when you save it. – workingdog support Ukraine May 20 '22 at 23:44