在Swift中从UIScrollView创建PDF文件 [英] Create a PDF file from a UIScrollView in Swift

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

问题描述

我想根据UIScrollView的内容创建PDF文件.

I want to create a PDF file from the contents of a UIScrollView.

func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String) {
    let pdfData = NSMutableData()
    let height = 1754.0
    let width = 1240.0
    UIGraphicsBeginPDFContextToData(pdfData, CGRect(x:-30, y:15,width:width,height:height) , nil)
    UIGraphicsBeginPDFPage()
    guard let pdfContext = UIGraphicsGetCurrentContext() else { return }

    aView.layer.render(in: pdfContext)
    UIGraphicsEndPDFContext()

    if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
        let documentsFileName = documentDirectories + "/" + fileName
        debugPrint(documentsFileName)
        pdfData.write(toFile: documentsFileName, atomically: true)
    }
}

我希望代码会生成一个基于滚动视图中内容大小的PDF.

I would expect that the code generates a PDF oriented on the size of the content in the scrollview.

该代码当前仅生成静态PDF.如果滚动视图中的内容大于PDF页面,则该内容将被剪切.

The code currently only generates a static PDF. If the content in the scrollview is bigger than the PDF page, the content is cut off.

推荐答案

我可以使用此Cocoapods框架实现此目的.就我而言,我想从Spreadsheet制作一个PDF,以便该框架帮助我添加具有不同列大小的表以及我希望从数据中获取的更多功能.

I could achieve this using this Cocoapods framework. In my case I wanted to make a PDF from Spreadsheet , so that this framework help me to add table with different column size and many more features which I had wanted from my data.

https://cocoapods.org/pods/SimplePDF

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

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