如何在Swift中将String转换为PDF

时间:2017-03-09 17:16:16

标签: pdf

您能告诉我如何将此字符串转换为PDF ...

%PDF-1.3%ãÏÏ20 ob​​j&lt;&gt; streams8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-! !!!!s8W- s8W- s8W- s8W- s8W- s8W- s8W- s8W&安培; TP(9o5Q:] !!<3$!!#7!!!!'"p](9o&,lP/!! K(!!#4 !!!'"p](9o#P\9!!! K(!!# 1 _!' “p](90” 8Dir!K(!!#1_!!!'"p](9o"7Q9j!!<3$!!#+]!!!'"p](9o!T3qX!!<3$!!"tY!!!'"p^dE(!T5("s8N3$s8Vj!s8W-!p {KP9J {1}} {KP9J {1}} {KP9J {1}} KP9!T63Bs8N3 $ s8Vj)s8W-!p](90 !T3qX “?9 / $ !! DUR !! 的'p](90 Up'h .OtK !!ë! - !!!!”!,@ p](显示90“7Q9j'^ g {{ 1}} K(!! E7O !!!! - $ p](显示90 WTq8 !! &cms8N3$s8Vj)s8W-!p KP9JH,ZLrW &cms8N3$s8Vj9s8W-!p KP9JH,ZLrW &cms8N3$s8Vj)s8W-!p KP9JH,ZLR ?(s8W- s8N- “p !!E-!!!!Q0p](9o"8Dir!$;1@!!E3#!!!9(p](9o"8i-!!"T&0!!E6$!!!-$p](9o!WE'!!! K(!” TY!“T&放大器; 0 !!”泰!!! - $ p](90! “p](90 T3qX!”! !T3qX “T&安培; 0 !!” TY !!! - $ p](显示90 “5j.Z $;!1 @ !!”!TY !!! 9(p](90#N,R ^'^ g K(!!E8j!!!'"p^dE)!<<*!r!*?(s8W-!s8N-"p p](显示9oJ,fQKs8W-!s8Vurs8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W-!s8W ! - s8W- s8W- s8W- s8W- s8W- s8W- s8W- s8W! - !!!

2 个答案:

答案 0 :(得分:1)

这不是一个完整的pdf文件,您缺少%% EOF,并且单个PDF中可能有多个%% EOF。您还错过了对象的结尾和流的结尾。假设您将完整的pdf文件作为字符串,您必须知道使用什么编码来获取该字符串,并使用相同的编码将其写回二进制(十六进制)文件。

这是以下内容的重复: How can I make a PDF file using base64 string ? Swift

答案 1 :(得分:0)

这是一个完整的示例,说明如何存储,获取和共享base64编码的PDF 我希望它会有用。

import UIKit

class ViewController: UIViewController {


@IBAction func sharePDFActionButton(_ sender: Any) {
    do {
        try savePdf()
        loadPDFAndShare()
    } catch  {
        print("FALLO EL GUARDAR EL PDF")
    }

}

let pdfString = "PUT ANY BASE64 ENCODED PDF FILE"
override func viewDidLoad() {
    super.viewDidLoad()
}

func savePdf() throws {
    let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let pdfDocURL = documentsURL.appendingPathComponent("document.pdf")
    let pdfData = Data(base64Encoded: pdfString)
    try pdfData!.write(to: pdfDocURL)
}

func loadPDFAndShare(){
    do {
        let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        let pdfDocURL = documentsURL.appendingPathComponent("document.pdf")

        let document = NSData(contentsOf: pdfDocURL)
        let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [document!], applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView=self.view
        present(activityViewController, animated: true, completion: nil)
        print("document was not found")
    } catch  {
        print("document was not found")
    }
  }
}

最诚挚的问候。