如何在Swift中创建PDF / A文件?

时间:2016-08-23 14:48:39

标签: ios swift pdf

我想在我的iOS应用中创建一个PDF / A文件但不知道从哪里开始。

我的实际代码(生成一个简单的PDF)是:

_ = UIGraphicsBeginPDFContextToFile(temporaryPdfFilePath, paperSize, nil)
UIGraphicsBeginPDFPageWithInfo(paperSize, nil)
let currentContext: CGContextRef = UIGraphicsGetCurrentContext()!

... ...绘制

UIGraphicsEndPDFContext()

是否有任何API或其他内容可供使用?

4 个答案:

答案 0 :(得分:6)

准备html文件,然后转换为pdf。 Swift 3.0版

func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
    let data = NSMutableData()
    UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)
    UIGraphicsBeginPDFPage()
    printPageRenderer.drawPage(at: 0, in: UIGraphicsGetPDFContextBounds())
    UIGraphicsEndPDFContext()
    return data
}

func exportHTMLContentToPDF(HTMLContent: String) {
    let printPageRenderer = CustomPrintPageRenderer()
    let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
    printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
    let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)
    let pdfFilename = getDocumentsDirectory().appendingPathComponent("nameOfPDFfile.pdf")

    pdfData?.write(to: pdfFilename, atomically: true)
    }
}

精彩教程:http://www.appcoda.com/pdf-generation-ios/

答案 1 :(得分:1)

有很多pod可以轻松创建PDFS。我刚刚使用了SimplePDF,这很轻松。

SimplePDF:https://github.com/nRewik/SimplePDF

我希望这有助于某人。

答案 2 :(得分:0)

//
//  ViewController.swift
//  Ganrate_pdf
//
//  Created by Tops on 3/23/17.
//  Copyright © 2017 tops. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var web_view: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
            // create html code
            let html = "<b>Welcome <i>World!</i></b> <p><h1><font color='red'>This  is PDF file In swift 3.0</font></h1></p>"
            let frmt = UIMarkupTextPrintFormatter(markupText: html)

            //  set print format

            let render = UIPrintPageRenderer()
            render.addPrintFormatter(frmt, startingAtPageAt: 0)

            //  Create Paper Size for print

            let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8)
            let printable = page.insetBy(dx: 0, dy: 0)

            render.setValue(NSValue(cgRect: page), forKey: "paperRect")
            render.setValue(NSValue(cgRect: printable), forKey: "printableRect")

            // 4. Create PDF context and draw

            let filedata = NSMutableData()
            UIGraphicsBeginPDFContextToData(filedata, CGRect.zero, nil)

            for i in 1...render.numberOfPages {

                UIGraphicsBeginPDFPage();
                let bounds = UIGraphicsGetPDFContextBounds()
                render.drawPage(at: i - 1, in: bounds)
            }

            UIGraphicsEndPDFContext();

            // 5. Save PDF file you can also save this file by using another button


            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

            filedata.write(toFile: "\(documentsPath)/myfile.pdf", atomically: true)

           }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

// Button on view for show the file  in webview
    @IBAction func Ganratepdf(_ sender: UIButton)
    {
        let file="myfile"
    self.ganarate_pdf(filename: file)
    }

    func ganarate_pdf(filename:String)
    {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = "\(documentsPath)/\(filename).pdf"
        let url = URL(fileURLWithPath: filePath)

        let urlRequest = URLRequest(url: url)
        web_view.loadRequest(urlRequest)


    }
}

答案 3 :(得分:0)

我写了一个使用HTML模板创建PDF的窗格:

https://github.com/NGromann/SwiftHtmlPdf

如果usage guide没有帮助,您也可以看看example project