我需要生成 pdf 格式的报告并打印它。我生成一个包含所有信息的 View 并将其转换为 pdf,然后在 UIWebView
中显示 pdf 文件。但是当我在UIWebView
中看到Pdf文件时,该文件不完整(右侧区域和机器人区域被剪切了很大一部分)。我不知道当我将其转换为 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。
最佳答案
我以编程方式生成 View ,因此当执行此行时:
UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil)
框架没有渲染或者什么的。因此,我使用了具有我想要的大小的 CGRect 并且它可以工作:
UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 2480, height: 3508), nil)
关于ios - 从 UIView 转换后的 PDF 不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33501029/