Reputation: 4645
Is there any way or any libraries to create a PDF from a UIView preserving the text ie with the text selectable. I have have tried the following but the clarity is compromised as it is rendered as a bitmap.
+ (NSData *)pdfDataOfScrollView:(UITableView *)scrollView
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, scrollView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[scrollView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
return pdfData;
}
Upvotes: 1
Views: 982
Reputation: 1357
try this code
NSMutableData *pdfData=[NSMutableData data];
CGRect bounds = CGRectMake(0, 0, 612, 792); // letter sized paper, adjust as needed
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();
[aView.layer renderInContext:pdfContext];
UIGraphicsBeginPDFPage();
[bView.layer renderInContext:pdfContext2];
UIGraphicsEndPDFContext();
Upvotes: 0
Reputation: 13514
You can use this library to create PDF Link.
Using above library you can generate PDF from below code.
func generatePDF() {
let v1 = UIScrollView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
let v3 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
v1.backgroundColor = .red
v1.contentSize = CGSize(width: 100.0, height: 200.0)
v2.backgroundColor = .green
v3.backgroundColor = .blue
let dst = URL(fileURLWithPath: NSHomeDirectory().stringByAppendingString("/sample1.pdf"))
// outputs as Data
do {
let data = try PDFGenerator.generated(by: [v1, v2, v3])
data.write(to: dst, options: .atomic)
} catch (let error) {
print(error)
}
// writes to Disk directly.
do {
try PDFGenerator.generate([v1, v2, v3], to: dst)
} catch (let error) {
print(error)
}
}
Upvotes: 1