ios – Scrollview Content material to PDF

0
23
ios – Scrollview Content material to PDF


extension UIScrollView {
    
          func PDFWithScrollView() -> Information? {
            let pageMargin: CGFloat = 10.0
    
            // Improve web page measurement to accommodate margins
            let pageDimensions = self.bounds
            let pageSize = CGSize(width: pageDimensions.measurement.width + 2 * pageMargin,
                                  peak: pageDimensions.measurement.peak + 2 * pageMargin)
            let totalSize = self.contentSize
    
            // Variety of pages required vertically
            let numberOfPagesVertically = Int(ceil(totalSize.peak / (pageSize.peak - 2 * pageMargin)))
    
            let outputData = NSMutableData()
            UIGraphicsBeginPDFContextToData(outputData, CGRect(origin: .zero, measurement: pageSize), nil)
    
            let savedContentOffset = self.contentOffset
            let savedContentInset = self.contentInset
    
            self.contentInset = UIEdgeInsets.zero
    
            if let context = UIGraphicsGetCurrentContext() {
                for indexVertical in 0 ..< numberOfPagesVertically {
                    UIGraphicsBeginPDFPage()
    
                    // Set the content material offset for the present web page
                    let offsetVertical = CGFloat(indexVertical) * (pageSize.peak - 2 * pageMargin)
                    self.contentOffset = CGPoint(x: 0, y: offsetVertical)
    
                    // Alter context for margins
                    context.translateBy(x: pageMargin, y: -offsetVertical + pageMargin)
    
                    // Render the content material
                    self.layer.render(in: context)
    
    
                }
            }
    
            UIGraphicsEndPDFContext()
    
            self.contentInset = savedContentInset
            self.contentOffset = savedContentOffset
    
            return outputData as Information
        }
}

I’m utilizing above code to transform my scrollview content material to pdfData which is working effective downside is my some textual content break up into two pages half-half as seen in connected screenshot can I resolve it.

present output

I attempt to add margins to however appears no luck as copy as certain sensible not content material sensible so this creating subject.

LEAVE A REPLY

Please enter your comment!
Please enter your name here