<code>extension UIScrollView {
func PDFWithScrollView() -> Data? {
let pageMargin: CGFloat = 10.0
// Increase page size to accommodate margins
let pageDimensions = self.bounds
let pageSize = CGSize(width: pageDimensions.size.width + 2 * pageMargin,
height: pageDimensions.size.height + 2 * pageMargin)
let totalSize = self.contentSize
// Number of pages required vertically
let numberOfPagesVertically = Int(ceil(totalSize.height / (pageSize.height - 2 * pageMargin)))
let outputData = NSMutableData()
UIGraphicsBeginPDFContextToData(outputData, CGRect(origin: .zero, size: 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 offset for the current page
let offsetVertical = CGFloat(indexVertical) * (pageSize.height - 2 * pageMargin)
self.contentOffset = CGPoint(x: 0, y: offsetVertical)
// Adjust context for margins
context.translateBy(x: pageMargin, y: -offsetVertical + pageMargin)
// Render the content
self.layer.render(in: context)
}
}
UIGraphicsEndPDFContext()
self.contentInset = savedContentInset
self.contentOffset = savedContentOffset
return outputData as Data
}
}
</code>
<code>extension UIScrollView {
func PDFWithScrollView() -> Data? {
let pageMargin: CGFloat = 10.0
// Increase page size to accommodate margins
let pageDimensions = self.bounds
let pageSize = CGSize(width: pageDimensions.size.width + 2 * pageMargin,
height: pageDimensions.size.height + 2 * pageMargin)
let totalSize = self.contentSize
// Number of pages required vertically
let numberOfPagesVertically = Int(ceil(totalSize.height / (pageSize.height - 2 * pageMargin)))
let outputData = NSMutableData()
UIGraphicsBeginPDFContextToData(outputData, CGRect(origin: .zero, size: 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 offset for the current page
let offsetVertical = CGFloat(indexVertical) * (pageSize.height - 2 * pageMargin)
self.contentOffset = CGPoint(x: 0, y: offsetVertical)
// Adjust context for margins
context.translateBy(x: pageMargin, y: -offsetVertical + pageMargin)
// Render the content
self.layer.render(in: context)
}
}
UIGraphicsEndPDFContext()
self.contentInset = savedContentInset
self.contentOffset = savedContentOffset
return outputData as Data
}
}
</code>
extension UIScrollView {
func PDFWithScrollView() -> Data? {
let pageMargin: CGFloat = 10.0
// Increase page size to accommodate margins
let pageDimensions = self.bounds
let pageSize = CGSize(width: pageDimensions.size.width + 2 * pageMargin,
height: pageDimensions.size.height + 2 * pageMargin)
let totalSize = self.contentSize
// Number of pages required vertically
let numberOfPagesVertically = Int(ceil(totalSize.height / (pageSize.height - 2 * pageMargin)))
let outputData = NSMutableData()
UIGraphicsBeginPDFContextToData(outputData, CGRect(origin: .zero, size: 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 offset for the current page
let offsetVertical = CGFloat(indexVertical) * (pageSize.height - 2 * pageMargin)
self.contentOffset = CGPoint(x: 0, y: offsetVertical)
// Adjust context for margins
context.translateBy(x: pageMargin, y: -offsetVertical + pageMargin)
// Render the content
self.layer.render(in: context)
}
}
UIGraphicsEndPDFContext()
self.contentInset = savedContentInset
self.contentOffset = savedContentOffset
return outputData as Data
}
}
I am using above code to convert my scrollview content to pdfData which is working fine problem is my some text split into two pages half-half as seen in attached screenshot can I resolve it.
current output
I try to add margins to but seems no luck as copy as bound wise not content wise so this creating issue.
New contributor
Jaydeep Vyas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.