在ios上生成高分辨率的pdf

时间:2013-03-07 22:15:05

标签: ios pdf retina-display

有没有办法在支持视网膜显示的ios上生成高分辨率PDF?我制作了一个基本的PDF生成器,但在我的iPad 3上它仍然看起来像素化。感谢您提供各种建议!

更新

在我的PDF文件中,文字流畅而美观。但是当我使用代码绘制这个pdf时,我在视网膜显示器上得到像素化(绘图代码在底部)。

PDF生成:

    NSString *fileName = [self.bookManager cacheBookFileForPage:i];
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    CGContextTranslateCTM(currentContext, 0, 100);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    // TODO: Temporary numbers
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 768 * 2, 1024 * 2), nil);


    NSString *textToDraw = @"text";
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

    CTFontRef ctFont= CTFontCreateWithName(CFSTR("Arial"), 20 * 2, &CGAffineTransformIdentity);

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                (__bridge id)ctFont, (NSString *)kCTFontAttributeName,
                                nil];

    NSAttributedString *string = [[NSAttributedString alloc] initWithString:textToDraw
                                                                 attributes:attributes];

    CFRelease(ctFont);


    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);

    // TODO: temporary        
    CGRect frameRect = CGRectMake(100, 100, 800 * 2, 50 * 2);
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    CFRelease(frameRef);
    CFRelease(stringRef);
    CFRelease(framesetter);

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();

PDF绘图:

// This will return pageRef from PDF
CGPDFPageRef page = [self.bookManager contentForPage:index + 1];

CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));
CGContextTranslateCTM(context, 0.0f, [self.view.layer bounds].size.height);

CGRect transformRect = CGRectMake(0, 0, [self.view.layer bounds].size.width, [self.view.layer bounds].size.height);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, transformRect, 0, true);

// And apply the transform.
CGContextConcatCTM(context, pdfTransform);

CGContextScaleCTM(context, 1.0f, -1.0f);

CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

CGContextDrawPDFPage(context, page);

1 个答案:

答案 0 :(得分:0)

我认为你遇到的一个问题是你正在缩放你所画的东西;不要使用这种转变!只需创建你的pdf上下文并在其中进行本地绘制。你的字体看起来很清晰,因为它们现在以全分辨率绘制,而不是一半rez,然后该字体的位图按比例放大,这当然会引入锯齿。

tl; dr:直接绘制到pdf,不要在文本上使用缩放变换。