【问题标题】:Pdf is incomplete when converted from UIView从 UIView 转换时 Pdf 不完整
【发布时间】:2016-02-03 17:14:16
【问题描述】:

我需要生成 pdf 格式的报告并打印出来。我生成包含所有信息的视图并将其转换为 pdf,然后在 UIWebView 中显示 pdf 文件。但是当我看到UIWebView中的pdf文件时,文件并不完整(右边区域和bot区域的很大一部分被切掉了)。我不知道我的UIView 是否在我将其转换为 Pdf 时太大或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。

【问题讨论】:

    标签: ios swift uiview pdf-generation


    【解决方案1】:

    我正在以编程方式生成视图,因此在执行此行时:

    UIGraphicsBeginPDFContextToData(pdfData, aView.frame, nil)
    

    帧没有被渲染或者什么的。因此,我使用了我想要的大小的 CGRect 并且它可以工作:

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

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 2018-10-04
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2011-11-25
      • 2011-07-23
      • 1970-01-01
      相关资源
      最近更新 更多