Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8576949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:02:58+00:00 2026-06-11T20:02:58+00:00

Generate PDF from UIView by rendering looses quality iOS I have a custom UIView

  • 0

Generate PDF from UIView by rendering looses quality iOS

I have a custom UIView called TTT_WantsToBeRazorSharpView.
This view does nothing, but draw a text with

NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@"");
CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]);

Now the View is drawn three times to an UIViewController (one time in IB) and two times in code with these lines (compare to image below):

 TTT_WantsToBeRazorSharpView *customViewSharp = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:CGRectMake(10,250, 300, 80)];
 [self.view addSubview:customViewSharp];

 TTT_WantsToBeRazorSharpView *customViewBlurryButIKnowWhy = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:CGRectMake(361.5, 251.5, 301.5, 80.5)];
 [self.view addSubview:customViewBlurryButIKnowWhy];

the first code drawn view is razorsharp, while the second isn’t, but that is ok, because of rect’s comma values (361.5, 251.5, 301.5, 80.5).

See this picture:
See this picture

But my problem is now, if I render the view into an pdf document it is blurry!
And don’t know why, see here:
blurry pdf

and the PDF file itself Test.pdf
https://raw.github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality/master/Test.pdf

and the lines to render the view into the pdf:

//this is blurry, but why? it can't be the comma coordinates
CGRect customFrame1 = CGRectMake(10,50, 300, 80);
TTT_WantsToBeRazorSharpView *customViewSharp = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:customFrame1];
CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, (int)customFrame1.origin.x, (int)customFrame1.origin.y);
[customViewSharp.layer renderInContext:context];
CGContextRestoreGState(context);

So why is the renderInContext:context text blurry?

I appreciate all your hints and help, I even made an GitHub project for the brave of you (with source): https://github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:03:00+00:00Added an answer on June 11, 2026 at 8:03 pm

    Try this:

    - (void)initValues {
        UIColor *gray = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f];
        CALayer *l = [self layer];
        l.masksToBounds = YES;
        l.cornerRadius = 10.0f;
        l.borderWidth = 3.0f;
        self.backgroundColor = gray;
        l.backgroundColor = gray.CGColor;
        l.borderColor = gray.CGColor;
    }
    
    - (void)drawRect:(CGRect)rect {
        NSLog(@"TTT_WantsToBeRazorSharpView drawRect[%3.2f,%3.2f][%3.2f,%3.2f]",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
    
        // get the graphic context
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetShouldSmoothFonts(ctx,YES);
    
        CGColorRef colorWhite = CGColorRetain([UIColor redColor].CGColor);
    
        CGContextSetFillColorWithColor(ctx, colorWhite);
    
        CGContextSelectFont(ctx, "Helvetica-Bold", 24, kCGEncodingMacRoman);
        CGAffineTransform tranformer = CGAffineTransformMakeScale(1.0, -1.0);
        CGContextSetTextMatrix(ctx, tranformer);
    
        //why is this sucker blurry?
        NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@"");
        CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]);
        CGColorRelease(colorWhite);
    }
    
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    
        if (!layer.shouldRasterize && !CGRectIsEmpty(UIGraphicsGetPDFContextBounds())) {
            [self drawRect:self.bounds];
        }
        else {
            [super drawLayer:layer inContext:ctx];
        }
    }
    

    I had to change the text color to red to make it visible. Because the text is written first, then the transparent grey button is placed over it, it would make the white text disappear. When adding the drawLayer method, everything that does not need to be rasterized is written as vector to pdf, making the text also selectable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate on-the-fly PDF reports from Java objects. I have not
I have a website that generates pdf file from CSP. Will this pdf file
I have an iPad app and I'm trying to generate a PDF from a
I need to dinamicaly generate a PDF from HTML, but I have PDF Support
I'm trying to generate a pdf from template using this snippet: def write_pdf(template_src, context_dict):
I have been using fop 0.95 to generate pdf files from xml data. I
I have written code to generate thumbnails from pdf files & save them as
I am using the Rendering plugin to generate a PDF from within a Web
I'm using Prawn to generate my PDF files from a HTML source. This HTML
I am using Flying Saucer to generate pdf from webpages . I came across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.