如何在iOS上生成带有“真实”文本内容的PDF?

我想在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,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一堆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显示它实际上是位图图像.

有什么建议?

解决方法

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...