我想在我的iOS 6应用程序中生成一个漂亮的PDF。如何在iOS上使用“真实”文本内容生成PDF?
我已经试过:
- UIView的渲染方面
- 使用CoreText
- 使用的NSString drawInRect
- 使用的UILabel的drawRect
下面是一个代码示例:
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(NSString *) path
{
CGContextRef myOutContext = NULL;
NSURL * url;
url = [NSURL fileURLWithPath:path];
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL ((__bridge CFURLRef) url,
&inMediaBox,
NULL);
}
return myOutContext;
}
-(void)savePdf:(NSString *)outputPath
{
if (!pageViews.count)
return;
UIView * first = [pageViews objectAtIndex:0];
CGContextRef pdfContext = [self createPDFContext:CGRectMake(0, 0, first.frame.size.width, first.frame.size.height) path:outputPath];
for(UIView * v in pageViews)
{
CGContextBeginPage (pdfContext,nil);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, (int)(v.frame.size.height));
transform = CGAffineTransformScale(transform, 1, -1);
CGContextConcatCTM(pdfContext, transform);
CGContextSetFillColorWithColor(pdfContext, [UIColor whiteColor].CGColor);
CGContextFillRect(pdfContext, v.frame);
[v.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);
}
CGContextRelease (pdfContext);
}
个
那些只呈现该UIViews包含一个UIImageView +一堆UILabels的(部分带和一些无国界)。
我也试过计算器上发现了一个建议:继承的UILabel和这样做:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds());
if (!layer.shouldRasterize && isPDF)
[self drawRect:self.bounds]; // draw unrasterized
else
[super drawLayer:layer inContext:ctx];
}
但是,这并不会改变任何事情。
不管我做什么,在预览打开PDF时,文本部分是可选的模块,但不是每个字符的字符,和缩放的PDF显示它实际上是一个位图图像。
有什么建议吗?
你可以发布一个示例代码片段和输出PDF文件,所以我可以看看吗? – iPDFdev
你有没有想过这个?体验“块选择”问题。 – JeremyDay
似乎并没有与现在的情况发生: 'UIGraphicsBeginPDFContextToFile(outputPath,view.bounds,无); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext();' – JeremyDay