从 pdf 数据中以 iOS 显示 PDF
Show PDF in iOS from pdf data
我正在尝试使用 Swift 在我的 iOS 应用程序中显示 pdf。我从服务器得到的响应是 pdf 数据,它看起来像这样:
%PDF-1.4 %ÓôÌá 1 0 obj << /CreationDate(D:20180403085232+02'00') /Creator(PDFsharp 1.32.3057-g (www.pdfsharp.net)) /Producer(PDFsharp 1.32.3057-g (www.pdfsharp.net)) endobj 2 0 obj << /Type/Catalog /Pages 3 0 R endobj 3 0 obj << /Type/Pages /Count 3 /Kids[4 0 R 8 0 R 11 0 R] endobj 4 0 obj << /Type/Page /MediaBox[0 0 612 792] /Parent 3 0 R /Contents 5 0 R /Resources << /ProcSet [/PDF/Text/ImageB/ImageC/ImageI] /XObject << /I0 6 0 R /ExtGState << /GS0 7 0 R
/Group << /CS/DeviceRGB /S/Transparency /I false /K false
endobj 5 0 obj << /Length 260 /Filter/FlateDecode stream xœíRMK1½÷Wä˜M“&Mî¢àa]oâIEDF‚¿ÞÌ̺²ø¥”4y¯—´C©@¹ŽÆЃáú!ÏDpñZ†2€š W9#¾gNŽa“/…à4÷}©gåòŠàf§{¾.«õ–àWìÚ¡WÆT4Bµ †¡O·e[F>g^í=|UEw‡ª‚Ö$å(U‘ôÇ›„L¶ÜÑ™—~¤6•š'Ú9f”Pz›PŽ€¦¨"бv_gly›Û÷Mä~.î] ™Ûë؈?œå€«Ul¡À™y_6þ»Aÿ—ÿ¤|èYòÿX`ìfÈ»ï³ÉõŠ¹·ò endstream endobj 6 0 obj << /Type/XObject /Subtype/Image /Length 5097196 /Filter/FlateDecode /Width 2511 /Height 3531 /BitsPerComponent 8 /ColorSpace/DeviceRGB /Interpolate true stream xœìÝ œ¤g]'ðïé9rÂáˆJAE%€ Š"ë‚FQ0 Ë!º» ¢‚+^ (h
我检查了 iOS-PDF-Reader 和 PDFKit,但主要问题是我不明白如何处理应用程序中的响应。
fileprivate func loadPdf() {
startIndication()
var session: URLSession!
let sessionConfig = URLSessionConfiguration.default
session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
guard let URL = URL(string: "http://localhost:3000/pdf-download") else { return }
let token = "Bearer " + self.user!.token!
var request = URLRequest(url: URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(token, forHTTPHeaderField: "Authorization")
var invData:[String:Any] = [String:Any]()
invData["INVID"] = invoice.ID
request.httpBody = try! JSONSerialization.data(withJSONObject: invData, options: [])
let task = session.dataTask(with: request) { (data:Data?, response: URLResponse?, error:Error?) in
if error == nil {
guard let data = data else { return }
// I get the data but I cannot do anything with it
} else {
print("response: \(String(describing: error))")
}
}
task.resume()
}
有什么想法吗?谢谢!
如果您查看 PDFReader 的文档,您可以使用数据创建 PDF,并使用 PDFViewController 显示它:
guard let data = data else { return }
let document = PDFDocument(fileData: data, fileName: "Sample PDF")!
//Display a PDFDocument
let readerController = PDFViewController.createNew(with: document)
navigationController?.pushViewController(readerController, animated: true)
或来自 Apple 的 PDFKit:
guard let data = data else, let doc = PDFDocument(data: data) { return }
let pdfView = PDFView()
pdfView.document = doc
/* present the View */
我正在尝试使用 Swift 在我的 iOS 应用程序中显示 pdf。我从服务器得到的响应是 pdf 数据,它看起来像这样:
%PDF-1.4 %ÓôÌá 1 0 obj << /CreationDate(D:20180403085232+02'00') /Creator(PDFsharp 1.32.3057-g (www.pdfsharp.net)) /Producer(PDFsharp 1.32.3057-g (www.pdfsharp.net)) endobj 2 0 obj << /Type/Catalog /Pages 3 0 R endobj 3 0 obj << /Type/Pages /Count 3 /Kids[4 0 R 8 0 R 11 0 R] endobj 4 0 obj << /Type/Page /MediaBox[0 0 612 792] /Parent 3 0 R /Contents 5 0 R /Resources << /ProcSet [/PDF/Text/ImageB/ImageC/ImageI] /XObject << /I0 6 0 R /ExtGState << /GS0 7 0 R /Group << /CS/DeviceRGB /S/Transparency /I false /K false endobj 5 0 obj << /Length 260 /Filter/FlateDecode stream xœíRMK1½÷Wä˜M“&Mî¢àa]oâIEDF‚¿ÞÌ̺²ø¥”4y¯—´C©@¹ŽÆЃáú!ÏDpñZ†2€š W9#¾gNŽa“/…à4÷}©gåòŠàf§{¾.«õ–àWìÚ¡WÆT4Bµ †¡O·e[F>g^í=|UEw‡ª‚Ö$å(U‘ôÇ›„L¶ÜÑ™—~¤6•š'Ú9f”Pz›PŽ€¦¨"бv_gly›Û÷Mä~.î] ™Ûë؈?œå€«Ul¡À™y_6þ»Aÿ—ÿ¤|èYòÿX`ìfÈ»ï³ÉõŠ¹·ò endstream endobj 6 0 obj << /Type/XObject /Subtype/Image /Length 5097196 /Filter/FlateDecode /Width 2511 /Height 3531 /BitsPerComponent 8 /ColorSpace/DeviceRGB /Interpolate true stream xœìÝ œ¤g]'ðïé9rÂáˆJAE%€ Š"ë‚FQ0 Ë!º» ¢‚+^ (h
我检查了 iOS-PDF-Reader 和 PDFKit,但主要问题是我不明白如何处理应用程序中的响应。
fileprivate func loadPdf() {
startIndication()
var session: URLSession!
let sessionConfig = URLSessionConfiguration.default
session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
guard let URL = URL(string: "http://localhost:3000/pdf-download") else { return }
let token = "Bearer " + self.user!.token!
var request = URLRequest(url: URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(token, forHTTPHeaderField: "Authorization")
var invData:[String:Any] = [String:Any]()
invData["INVID"] = invoice.ID
request.httpBody = try! JSONSerialization.data(withJSONObject: invData, options: [])
let task = session.dataTask(with: request) { (data:Data?, response: URLResponse?, error:Error?) in
if error == nil {
guard let data = data else { return }
// I get the data but I cannot do anything with it
} else {
print("response: \(String(describing: error))")
}
}
task.resume()
}
有什么想法吗?谢谢!
如果您查看 PDFReader 的文档,您可以使用数据创建 PDF,并使用 PDFViewController 显示它:
guard let data = data else { return }
let document = PDFDocument(fileData: data, fileName: "Sample PDF")!
//Display a PDFDocument
let readerController = PDFViewController.createNew(with: document)
navigationController?.pushViewController(readerController, animated: true)
或来自 Apple 的 PDFKit:
guard let data = data else, let doc = PDFDocument(data: data) { return }
let pdfView = PDFView()
pdfView.document = doc
/* present the View */