Vinod Vishwanath
Vinod Vishwanath

Reputation: 5911

Save UIWebView content as A4 sized PDF

I have gone over previous questions touching upon the subject, but A4 Sized PDF is the troublesome part.

The code I have written below saves a proper (proportional) PDF file, but its size (area wise) is 4 times of A4-Size. If I halve the height and width, I get an A4 sized PDF but the content overflows into two pages.

How do I solve the problem? (fundamentally pertaining to scaling)

-(void)savePDFFromWebView:(UIWebView*)webView fileName:(NSString*)_fileName
{
    int height, width, header, sidespace;

    height = 1754;
    width = 1240;
    header = 15;
    sidespace = 30;

    // set header and footer spaces

    UIEdgeInsets pageMargins = UIEdgeInsetsMake(header, sidespace, header, sidespace);  

    webView.viewPrintFormatter.contentInsets = pageMargins;

    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];

    [renderer addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];

    CGSize pageSize = CGSizeMake(width, height);
    CGRect printableRect = CGRectMake(pageMargins.left,
                                      pageMargins.top,
                                      pageSize.width - pageMargins.left - pageMargins.right,
                                      pageSize.height - pageMargins.top - pageMargins.bottom);

    CGRect paperRect = CGRectMake(0, 0, pageSize.width, pageSize.height);

    [renderer setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
    [renderer setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];


    NSData *pdfData = [self printToPDFWithRender:renderer paperRect:paperRect];


    // save PDF file
    NSString *saveFileName = [NSString stringWithFormat:@"%@%dx%d.pdf", _fileName, (int)pageSize.width, (int)pageSize.height];

    NSString *savePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:saveFileName];
    [pdfData writeToFile: savePath  atomically: YES];
}

- (NSData*) printToPDFWithRenderer:(UIPrintPageRenderer*)renderer  paperRect:(CGRect)paperRect
{
    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData( pdfData, paperRect, nil );

    [renderer prepareForDrawingPages: NSMakeRange(0, renderer.numberOfPages)];

    CGRect bounds = UIGraphicsGetPDFContextBounds();

    for ( int i = 0 ; i < renderer.numberOfPages ; i++ )
    {
        UIGraphicsBeginPDFPage();

        [renderer drawPageAtIndex: i inRect: bounds];
    }

    UIGraphicsEndPDFContext();

    return pdfData;
}

Upvotes: 4

Views: 2852

Answers (1)

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

Here paperRect you providing is not of A4 size

CGRect paperRect = CGRectMake(0, 0,595.2,841.8);

According printableRect would change

CGRect printableRect = CGRectMake(sidespace,
                                  header,
                                  pageSize.width - (sidespace*2),
                                  pageSize.height - (header*2));

Also I think no need to specify contentInsets as printableRect will do the same. So remove below lines

UIEdgeInsets pageMargins = UIEdgeInsetsMake(header, sidespace, header, sidespace);  
webView.viewPrintFormatter.contentInsets = = pageMargins;

Upvotes: 2

Related Questions