【发布时间】:2015-04-19 01:42:30
【问题描述】:
在我的 iOS 应用程序中,我想从 UIWebView/UIView(包括子视图)创建一个 PDF。在我的应用程序中,我将首先在 UIWebView 中加载原始传入的 PDF,然后在 UIWebView 上添加图像作为子视图。我想使用此图像(子视图)从 UIWebview 创建一个 PDF,该图像具有原始清晰度且没有数据丢失。
PS:呈现的 PDF 中的图像应与 UIWebView 中的位置相同。
我能够从 UIWebView 创建 PDF,但它缺乏 PDF 清晰度并产生边框问题。
任何人都可以为从 UIWebView(包括子视图)呈现 PDF 提供一个清晰的解决方案吗?
编辑内容:
以上是 UIWebView 的截图。 Signature(test) 是子视图中的图像。我想将其呈现为清晰的 PDF,并且不会丢失任何数据。
在下面的答案中,UIPrintPageRenderer 从 UIWebView 呈现 PDF,但它忽略了 UIWebView 上方的子视图。这是此选项的主要问题。
使用 createPDFfromUIView 方法的另一个答案缺乏原始清晰度:
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename;
此方法也会出现边框问题。
我也尝试使用this参考中的以下代码直接在PDF上书写,而不截图。
- (void) drawCustomPDFContent
{
// Put your drawing calls here
// Draw a red box
[[UIColor redColor] set];
UIRectFill(CGRectMake(20, 20, 100, 100));
// Example of drawing your view into PDF, note that this will be a rasterized bitmap, including the text.
// To get smoother text you'll need to use the NSString draw methods
CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
}
- (void) createCustomPDF
{
NSURL* pdfURL = ... /* URL to pdf file */;
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
const size_t numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);
NSMutableData* data = [NSMutableData data];
UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
for(size_t page = 1; page <= numberOfPages; page++)
{
// Get the current page and page frame
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
// Draw the page (flipped)
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
CGContextDrawPDFPage(ctx, pdfPage);
CGContextRestoreGState(ctx);
if(page == 1)
{
[self drawCustomPDFContent];
}
}
UIGraphicsEndPDFContext();
CGPDFDocumentRelease(pdf);
pdf = nil;
// Do something with data here
[data writeToFile:... atomically:YES];
}
它完成了这项工作。但是,UIWebView 中的 (x,y) 坐标与原始 PDF 坐标不同,因此我无法映射确切的坐标以在 PDF 上进行绘制。
希望这能解决我的问题。请提出解决我的问题的方法。 如果不可能,请推荐符合我要求的iOS PDF kit/SDK。
【问题讨论】:
-
任何建议,请发表您的答案。
-
@Daij-Djan 在 UIWebView 上方手动添加子视图。
-
你应该解释一下你所说的“边界问题”是什么意思
标签: ios objective-c iphone pdf uiwebview