如何使用 base64 字符串制作 PDF 文件?迅速

How can I make a PDF file using base64 string ? Swift(如何使用 base64 字符串制作 PDF 文件?迅速)
本文介绍了如何使用 base64 字符串制作 PDF 文件?迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在我的项目目录中保存 PDF 文件的方法,我已经从 Web 服务收到了一个 base 64 pdf 字符串.我是否必须将其转换为 NSData 或类似的东西?

I'm looking for a way to save a PDF file on my project directory, I've received a base 64 pdf string from a Web Service yet. Do I have to convert it to NSData or something like that?

我是 Swift 编码的新手,但我可以按照您的指示进行操作.

I'm new at coding in Swift but I can follow your instructions.

我希望你能帮助我.谢谢

I hope you can help me. Thanks

推荐答案

是的,你必须将其转换为Data,然后将其保存到设备上的文档目录中.像这样的功能会起作用:

Yes, you have to convert it to Data and then save it to the documents directory on the device. A function like this would work:

func saveBase64StringToPDF(_ base64String: String) {

    guard
        var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last,
        let convertedData = Data(base64Encoded: base64String)
        else {
        //handle error when getting documents URL
        return
    }

    //name your file however you prefer
    documentsURL.appendPathComponent("yourFileName.pdf")

    do {
        try convertedData.write(to: documentsURL)
    } catch {
        //handle write error here
    }

    //if you want to get a quick output of where your 
    //file was saved from the simulator on your machine
    //just print the documentsURL and go there in Finder
    print(documentsURL)
}

这篇关于如何使用 base64 字符串制作 PDF 文件?迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)