是否有任何方法或任何库从UIView创建PDF保留文本,即文本可选。我已经尝试了以下但是清晰度受到了影响,因为它被渲染为位图。
+ (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;
}
答案 0 :(得分:1)
您可以使用此库创建PDF Link。
使用上面的库,您可以从下面的代码生成PDF。
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)
}
}
答案 1 :(得分:0)
试用此代码
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();