1

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

谢谢

4

1 回答 1

1

您链接到的字符串看起来好像是 base64 编码的。如果您对其进行解码,则结果应该是您可以使用的“%PDF”字符串。

于 2016-07-12T12:31:35.987 回答