PDF生成Swift [英] PDF Generation Swift

查看:132
本文介绍了PDF生成Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用swift生成pdf.下面的代码编译没有错误,但是,从未创建"xp.pdf"文档.非常感谢您的帮助.

I'm trying to generate a pdf using swift. The following code compiles without error, however, the "xp.pdf" document is never created. Your help is much appreciated.

import UIKit

import Foundation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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


    @IBAction func generatePDF(sender: AnyObject) {

        let pageSize:CGSize = CGSizeMake (850, 1100)
        let fileName: NSString = "xp.pdf"
        let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let documentDirectory: AnyObject = path.objectAtIndex(0)
        let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName)

        generatePDFs(pdfPathWithFileName)

        }

    func generatePDFs(filePath: String) {
        UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil)
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 850, 1100), nil)
        drawBackground()
        UIGraphicsEndPDFContext()




    }

    func drawBackground () {

       let context:CGContextRef = UIGraphicsGetCurrentContext()
        let rect:CGRect = CGRectMake(0, 0, 850, 1100)
        CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor)
        CGContextFillRect(context, rect)
    }

推荐答案

您是否有pdfkit? 根据我的理解,PSPDFKit可以很好地工作.您只需要从pspdfkit.com下载它,然后将PSPDFKit.framework拖放到应用程序目标的嵌入式二进制文件"部分即可. 不过,您确实需要为此付费,所以就可以了.我认为虽然有免费试用版,所以您可以尝试一下,看看这是否是您需要或想要的.

Do you have a pdfkit in the place? PSPDFKit works pretty well from what I understand. You just need to download it from pspdfkit.com, then drag and drop the PSPDFKit.framework into the "Embedded Binaries" section of your app's target. You do have to pay for it though, so theres that. I think there is a free trial for it though, so you can try it out and see if thats what you need or want.

这篇关于PDF生成Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆