从 UIView 转换过来的 Pdf 不完整
Pdf is incomplete when converted from UIView
我需要生成一份 pdf 格式的报告并打印出来。我正在生成包含所有信息的视图并将其转换为 pdf,然后在 UIWebView
中显示 pdf 文件。但是当我在UIWebView
中看到Pdf文件时,文件并不完整(右区和bot区的很大一部分被切掉了)。我不知道在将它转换为 Pdf 时我的 UIView
是否太大,或者 UIWebView
正在切割它。
PDF 函数
func createPDFfromUIView(aView: UIView) //-> UIImage
{
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil)
UIGraphicsBeginPDFPage()
let pdfContext = UIGraphicsGetCurrentContext();
aView.layer.renderInContext(pdfContext!)
UIGraphicsEndPDFContext();
let documentsDirectory: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask,true)[0] as NSString
let dataPath = documentsDirectory.stringByAppendingPathComponent("/JamaWEpdf")
if (!NSFileManager.defaultManager().fileExistsAtPath(dataPath as String)) {
do{
try NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil)
}
catch _{}
}
let filePath = "\(dataPath)entry\(entry1.entryId)pdf.pdf"
pdfData.writeToFile(filePath, atomically: true)
let webView = UIWebView()
webView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: filePath)))
webView.frame = self.view.frame
self.view.addSubview(webView)
}
不知道我的做法对不对。 UIView
尺寸为w:2480 h:3508.
我以编程方式生成视图,所以当执行此行时:
UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil)
框架没有渲染或什么的。所以我使用了我想要的大小的 CGRect 并且它有效:
UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 2480, height: 3508), nil)
我需要生成一份 pdf 格式的报告并打印出来。我正在生成包含所有信息的视图并将其转换为 pdf,然后在 UIWebView
中显示 pdf 文件。但是当我在UIWebView
中看到Pdf文件时,文件并不完整(右区和bot区的很大一部分被切掉了)。我不知道在将它转换为 Pdf 时我的 UIView
是否太大,或者 UIWebView
正在切割它。
PDF 函数
func createPDFfromUIView(aView: UIView) //-> UIImage
{
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil)
UIGraphicsBeginPDFPage()
let pdfContext = UIGraphicsGetCurrentContext();
aView.layer.renderInContext(pdfContext!)
UIGraphicsEndPDFContext();
let documentsDirectory: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask,true)[0] as NSString
let dataPath = documentsDirectory.stringByAppendingPathComponent("/JamaWEpdf")
if (!NSFileManager.defaultManager().fileExistsAtPath(dataPath as String)) {
do{
try NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil)
}
catch _{}
}
let filePath = "\(dataPath)entry\(entry1.entryId)pdf.pdf"
pdfData.writeToFile(filePath, atomically: true)
let webView = UIWebView()
webView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: filePath)))
webView.frame = self.view.frame
self.view.addSubview(webView)
}
不知道我的做法对不对。 UIView
尺寸为w:2480 h:3508.
我以编程方式生成视图,所以当执行此行时:
UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil)
框架没有渲染或什么的。所以我使用了我想要的大小的 CGRect 并且它有效:
UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 2480, height: 3508), nil)