【问题标题】:How can I generate a PDF with "real" text content on iOS?如何在 iOS 上生成带有“真实”文本内容的 PDF?
【发布时间】:2012-09-15 09:27:32
【问题描述】:

我想在我的 iOS 6 应用中生成好看的 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);
}

被渲染的 UIView 只包含一个 UIImageView + 一堆 UILabel(一些有边框,一些没有边框)。

我还尝试了在 stackoverflow 上找到的建议:子类化 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 文件,以便我看一下吗?
  • 你有没有想过这个问题?遇到“块选择”问题。
  • 现在似乎没有发生:UIGraphicsBeginPDFContextToFile( outputPath, view.bounds, nil ); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext();

标签: ios pdf uikit quartz-2d


【解决方案1】:

它与 UILabel 配合得很好,只是你必须解决一个错误:

Rendering a UIView into a PDF as vectors on an iPad - Sometimes renders as bitmap, sometimes as vectors

【讨论】:

    【解决方案2】:

    我去年这样做时的经验是,Apple 没有提供任何库来执行此操作。我最终导入了一个开源 C 库 (libHaru)。然后,我在视图层次结构的每个类中添加了一个用于向其输出的函数。任何带有子视图的视图都会在其子视图上调用渲染。我的 UILabel、UITextFields、UIImageViews、UISwitches 等会将它们的内容输出为文本或图形,因此我还为某些视图渲染了背景颜色。

    这并不是很令人生畏,但是 libHaru 给我的字体带来了一些问题,所以 iirc 我最终只使用了默认字体和字体大小。

    【讨论】:

    • 我会看看那个。谢谢
    【解决方案3】:

    【讨论】:

    • 我做了类似的事情,pdf看起来还可以,但是文本不是真实的文本,而是渲染的图像。我想生成一个带有可选文本的 pdf
    • 但我使用了相同的教程,它在缩放中也非常适合我。
    • 他的意思是可选择的——就像 Mac 上的光标一样(复制、粘贴等)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2023-03-18
    • 2020-10-21
    相关资源
    最近更新 更多