Create and store PDF document programmatically using Swift for iOS

前端 未结 2 1498
灰色年华
灰色年华 2020-12-04 20:35

I have an app that can view a PDF that is already stored within the project. I want to be able to create a new PDF document and store it in the app directory to later view

相关标签:
2条回答
  • 2020-12-04 21:00

    I used this code to create and save the file (using HTML)

    func createPDF() {
        let html = "<b>Hello <i>World!</i></b> <p>Generate PDF file from HTML in Swift</p>"
        let fmt = UIMarkupTextPrintFormatter(markupText: html)
    
        // 2. Assign print formatter to UIPrintPageRenderer
    
        let render = UIPrintPageRenderer()
        render.addPrintFormatter(fmt, startingAtPageAt: 0)
    
        // 3. Assign paperRect and printableRect
    
        let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
        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 pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, .zero, nil)
    
        for i in 1...render.numberOfPages {
            UIGraphicsBeginPDFPage();
            let bounds = UIGraphicsGetPDFContextBounds()
            render.drawPage(at: i - 1, in: bounds)
        }
    
        UIGraphicsEndPDFContext();
    
        // 5. Save PDF file
    
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
        pdfData.write(toFile: "\(documentsPath)/file.pdf", atomically: true)
    }
    

    Then I loaded it into UIWebView from the documents directory with this code:

    func loadPDF(filename: String) {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let url = URL(fileURLWithPath: documentsPath, isDirectory: true).appendingPathComponent(filename).appendingPathExtension("pdf")
        let urlRequest = URLRequest(url: url)
        webView.loadRequest(urlRequest)
    }
    
    0 讨论(0)
  • 2020-12-04 21:08

    For Swift 3:

    createPdf() {
        // 1. Create Print Formatter with input text.
    
        let formatter = UIMarkupTextPrintFormatter(markupText: textView.text)
    
        // 2. Add formatter with pageRender
    
        let render = UIPrintPageRenderer()
        render.addPrintFormatter(formatter, startingAtPageAt: 0)
    
    
        // 3. Assign paperRect and printableRect
    
        let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
        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 rect = CGRect.zero
    
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, rect, nil)
    
    
        for i in 1...render.numberOfPages {
    
            UIGraphicsBeginPDFPage();
            let bounds = UIGraphicsGetPDFContextBounds()
            render.drawPage(at: i - 1, in: bounds)
        }
    
        UIGraphicsEndPDFContext();
    
    
        // 5. Save PDF file
    
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
        pdfData.write(toFile: "\(documentsPath)/new.pdf", atomically: true)
    
        print("saved success")
    }
    
    0 讨论(0)
提交回复
热议问题