首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDF生成Swift

PDF生成Swift
EN

Stack Overflow用户
提问于 2014-11-21 06:06:51
回答 2查看 2.5K关注 0票数 0

我正试着用swift生成一个pdf。下面的代码编译时没有错误,但是永远不会创建"xp.pdf“文档。非常感谢您的帮助。

代码语言:javascript
复制
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)
    }
EN

回答 2

Stack Overflow用户

发布于 2017-10-20 01:42:33

你这里有pdfkit吗?据我所知,PSPDFKit运行得相当好。您只需从pspdfkit.com下载它,然后将PSPDFKit.framework拖放到应用程序目标的“嵌入式二进制文件”部分。你确实需要为它付钱,所以就是这样。我认为它有一个免费的试用版,所以你可以试一试,看看这是不是你需要的。

票数 1
EN

Stack Overflow用户

发布于 2016-04-12 01:43:10

对于Xcode 7 (swift 2.0),我必须进行以下更改才能使其正常工作:

替换:

代码语言:javascript
复制
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName)

通过以下方式:

代码语言:javascript
复制
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)

并解开UIGraphicsGetCurrentContext()

代码语言:javascript
复制
let context:CGContextRef = UIGraphicsGetCurrentContext()!

您还可以删除:

代码语言:javascript
复制
let pageSize:CGSize = CGSizeMake (850, 1100)

因为您从未使用过pageSize

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27050493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档