我String
从我们的 RPC 服务器收到一个包含 a 的响应。这String
是一个PDF
转换为String
. 在我的应用程序中,我需要将此字符串转换回PDF
文件。我在 Stack 上尝试了几个解决方案,但它们对我不起作用。
问题是我的函数不断抛出,因为它无法将我的字符串转换为CGPDFDocument
. 我在这里做错了什么?
这是我目前拥有的代码。
final class PdfGenerator: PdfGeneratorInterface {
// MARK: PdfGeneratorProtocol
func generatePdf(string: String) throws -> CGPDFDocument {
guard let
data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
cfData = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length)
else {
throw PdfGeneratorException.UnsupportedFormatException
}
let cgDataProvider = CGDataProviderCreateWithCFData(cfData)
guard let cgPDFDocument = CGPDFDocumentCreateWithProvider(cgDataProvider) else {
throw PdfGeneratorException.UnsupportedFormatException
}
return cgPDFDocument
}
}
PDF字符串内容:
https://gist.github.com/Combidi/fa53f2d74e7ae177bb3885d5d640c13c
谢谢