Я загружаю PDF с байтами. Я использую этот код, и он возвращает 0 байтов. Отлично работает на android
Alamofire.request(url.absoluteString, headers : nil)
.response { response in
print(response.data ?? "")
if let fileData = response.data {
successBlock(fileData)
}
}
// Create PDF
func createPDF( pdfData : Data) {
let render = UIPrintPageRenderer()
let html = "<b>Hello <i>World!</i></b> <p>Generate PDF file from HTML in Swift</p>"
let fmt = UIMarkupTextPrintFormatter(markupText: html)
render.addPrintFormatter(fmt, startingAtPageAt: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
let printable = page.insetBy(dx: 0, dy: 0)
render.setValue(NSValue(cgRect: page), forKey: "paperRect")
render.setValue(NSValue(cgRect: printable), forKey: "printableRect")
// 4. Create PDF context and draw
//let pointzero = CGPoint(x: 0,y :0)
let rect = CGRect.zero
let data = NSMutableData(base64Encoded: pdfData.base64EncodedData(), options: NSData.Base64DecodingOptions.ignoreUnknownCharacters);
UIGraphicsBeginPDFContextToData(data!, rect, nil)
for i in 1...render.numberOfPages {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPage(at: i - 1, in: bounds)
}
UIGraphicsEndPDFContext();
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
// 5. Save PDF file
do {
let fileURL = try documentsPath.asURL().appendingPathComponent("file.pdf")
try pdfData.write(to: fileURL, options: .atomic)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.viewPDF(filePath: fileURL.absoluteString)
}
} catch let error {
print(error)
self.showAlert("Error", message: "You already downloaded the contract. Please try again after 30 minutes.")
}
print("saved success")
}