我想在我的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