2015-11-03 90 views
0

我需要生成一个PDF报告打印它。我使用所有信息生成一个视图并将其转换为pdf,然后在UIWebView中显示pdf文件。但是当我看到UIWebView中的PDF文件时,该文件并不完整(正确区域和机器人区域的很大一部分被切断)。我不知道我的UIView在将它转换为PDF格式时太大,或者UIWebView正在切割它。从UIView转换为PDF时不完整

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

回答

0

我生成所述视图编程,所以当被执行这一行:

UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil) 

帧未被赋予或东西。因此,我使用的CGRect与我想要的大小和它的工作:

UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 2480, height: 3508), nil)