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))
}
}
try this:
or
Similarly for
PDFDocumentAttribute.subjectAttribute
.The above will set the
Title
of your document, and when you save it, thefile
name will be whateverfile name
you give it.EDIT-1: saving the
pdfDocument
to a file with a chosenfile name
.