【问题标题】:How do I generate a PDF containing views with dimensions larger than device screen?如何生成包含尺寸大于设备屏幕的视图的 PDF?
【发布时间】:2019-08-31 00:31:30
【问题描述】:

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

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

理想情况下,我需要能够创建更大的视图,并且仍然能够生成包含它们的 PDF。我正在设想一个画布,它可以根据其内容调整大小(缩小?)并保留其子视图的比例,以便在 Adob​​e 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]
    }
}

【问题讨论】:

    标签: ios swift uiview uiscrollview pdf-generation


    【解决方案1】:

    我认为如果您使用 UIGraphicsPDFRenderer 这可能会更好:

    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)
    }
    

    如果视图仍然超出 PDF 的范围,那么您可以缩放上下文:

    cgContext.scaleBy(x: 0.25, y:0.25)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-31
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多