Search code examples
iosobjective-cuiwebviewpdf-generationuigraphicscontext

Issues in generating PDF from uiwebview in objective c


I have open pdf file in uiwebview, add image and text in uiwebview subview.Then i tried created pdf file. Below sample i have used to generate pdf file. Multiple page writing in single page. How can i write sample quality and exact size. Please check screen shots and below sourceenter image description here

UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );

for (int i = 0; i < 10; i++) {

    CGRect f = [pdfView frame];
    [pdfView setFrame: f];

    UIGraphicsBeginPDFPage();
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(currentContext, 72, 72); // Translate for 1" margins
    [[[pdfView subviews] lastObject] setContentOffset:CGPointMake(0, 648 * i) animated:NO];
    [pdfView.layer renderInContext:currentContext];
}

UIGraphicsEndPDFContext();

and also i have tried this following link but i couldn't add uiwebview subview to write the pdf. http://b2cloud.com.au/tutorial/drawing-over-a-pdf-in-ios-pdf-template/


Solution

  • It looks to me like you're just trying to add content to an existing PDF, right?

    - (void) drawCustomPDFContent
    {
        //  Put your drawing calls here
        //  Draw a red box
        [[UIColor redColor] set];
        UIRectFill(CGRectMake(20, 20, 100, 100));
    
        //  Example of drawing your view into PDF, note that this will be a rasterized bitmap, including the text.
        //  To get smoother text you'll need to use the NSString draw methods
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:ctx];
    }
    
    - (void) createCustomPDF
    {
        NSURL* pdfURL = ... /* URL to pdf file */;
        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
    
        const size_t numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);
    
        NSMutableData* data = [NSMutableData data];
        UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
    
        for(size_t page = 1; page <= numberOfPages; page++)
        {
            //  Get the current page and page frame
            CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
            const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
    
            UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
    
            //  Draw the page (flipped)
            CGContextRef ctx = UIGraphicsGetCurrentContext();
            CGContextSaveGState(ctx);
            CGContextScaleCTM(ctx, 1, -1);
            CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
            CGContextDrawPDFPage(ctx, pdfPage);
            CGContextRestoreGState(ctx);
    
            if(page == 1)
            {
                [self drawCustomPDFContent];
            }
        }
    
        UIGraphicsEndPDFContext();
    
        CGPDFDocumentRelease(pdf);
        pdf = nil;
    
        //  Do something with data here
        [data writeToFile:... atomically:YES];
    }
    

    Parts referenced from:

    http://b2cloud.com.au/tutorial/drawing-over-a-pdf-in-ios-pdf-template/