如何生成包含尺寸大于设备屏幕的视图的PDF?

时间:2019-04-09 16:37:57

标签: ios swift uiview uiscrollview pdf-generation

我有几个UIViews,背景颜色清晰,黑色边框轮廓都根据我在长度,宽​​度和高度文本字段中输入的值进行了调整。这些视图是UIScrollView的子视图。调整视图大小后,我根据滚动视图的内容创建了PDF,并生成了可在Adobe Illustrator中使用和操纵的矢量化轮廓。

当我为视图使用小尺寸(即15 x 15)时,此过程可以正常工作,但是当我为目的使用更理想的尺寸(即3000 x 3000)时,视图会超出屏幕范围,从而导致生成的PDF仅包含一小部分视图。

理想情况下,我需要能够创建更大的视图,并且仍然能够生成包含它们的PDF。我正在设想一个画布,该画布将根据其内容调整大小(缩小?)并保留其子视图的比例,以便在Adobe Illustrator中查看时,PDF中的最终视图将按比例缩放。到目前为止,这是我的代码:

override func viewDidLoad() {
        super.viewDidLoad()

        lengthTextField.delegate = self
        widthTextField.delegate = self
        heightTextField.delegate = self
        scrollView.delegate = self

        scrollView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,UIView.AutoresizingMask.flexibleHeight]
        scrollView.minimumZoomScale = 1
        scrollView.maximumZoomScale = 50
        scrollView.zoomScale = 1

        self.setupGestureRecognizer()

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        scrollView.contentSize = CGSize(width: 5000, height: 5000)
    }

    func setupGestureRecognizer() {

    }

    @IBAction func viewPDF(_ sender: Any) {

        createPDFfrom(aView: self.scrollView.subviews[0], saveToDocumentsWithFileName: "MC.pdf")

        // Create and add a PDFView to the view hierarchy.
        let pdfView = PDFView(frame: self.scrollView.subviews[0].bounds)
        pdfView.autoScales = true
        view.addSubview(pdfView)

        // Create a PDFDocument object and set it as PDFView's document to load the document in that view.
        let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = (documentsDirectory as NSString).appendingPathComponent("MC.pdf") as String
        let pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))!
        pdfView.document = pdfDocument

        let document = NSData(contentsOfFile: filePath)

        let vc = UIActivityViewController(activityItems: [document as Any], applicationActivities: nil)
        self.present(vc, animated: true, completion: nil)

    }

    func createPDFfrom(aView: UIView, saveToDocumentsWithFileName fileName: String)
    {
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, 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)
        }
    }

    @IBAction func updateDimensions(_ sender: Any) {

        guard let length = NumberFormatter().number(from:
            lengthTextField.text ?? "") else { return }

        guard let width = NumberFormatter().number(from:
            widthTextField.text ?? "") else { return }

        guard let height = NumberFormatter().number(from:
            heightTextField.text ?? "") else { return }

        let flapHeight = CGFloat(truncating: width)/2

        let lengthFloat = CGFloat(truncating: length)
        let widthFloat = CGFloat(truncating: width)
        let heightFloat = CGFloat(truncating: height)

        UIView.animate(withDuration: 0.3) {
            self.faceAWidthConstraint.constant = lengthFloat
            self.faceAHeightConstraint.constant = heightFloat
            self.faceBWidthConstraint.constant = widthFloat
            self.faceA1HeightConstraint.constant = flapHeight
            self.view.layoutIfNeeded()
        }
    }

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return scrollView.subviews[0]
    }
}

1 个答案:

答案 0 :(得分:0)

我认为,如果您使用GROUP BY可能会更好:

UIGraphicsPDFRenderer

如果视图仍在PDF范围之外,则可以缩放上下文:

let url = URL(fileURLWithPath: filePath)
let pdfGenerator = UIGraphicsPDFRenderer(bounds: .init(x: 0, y: 0, width: 612, height: 792))
pdfGenerator.writePDF(to: url) { context in
  let cgContext = context.cgContext
  aView.layer.render(in: cgContext)
}