0

I am programmatically creating a PDF file from a detail view with a title and body and need to do this for multiple PDF pages if the text is too long for one page.

What I've tried so far is to create an array of the words in the cleanText and loop through checking the height each time and when big enough for a page, create a new page.

Here is what I have:

- (void) generatePdf: (NSString *)thefilePath :(NSString *) theHeader :(NSString *) theText :(UIImage *) theImage
{

    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

    int currentPage = 0;

    // maximum height and width of the content on the page, byt taking margins into account.
    CGFloat maxWidth = kDefaultPageWidth - 2*kBorderInset-2*kMarginInset;
    CGFloat maxHeight = kDefaultPageHeight - 2*kBorderInset - 2*kMarginInset;

    UIFont *font = [UIFont systemFontOfSize:14.0];
    CGFloat currentPageY = 0;

    NSString *cleanText;
    const char *utfString = [theText UTF8String];
    printf ("Converted string = %s\n", utfString);
    cleanText = [NSString stringWithUTF8String:utfString];

    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
    currentPageY = kMarginInset;

    // iterate through the words, adding to the pdf each time.
    NSArray *words = [cleanText componentsSeparatedByString:@" "];
    NSString *word;

    for (word in words)
    {
        // before we render any text to the PDF, we need to measure it, so we'll know where to render the next line.
        CGSize size = [word sizeWithFont:font constrainedToSize:CGSizeMake(maxWidth, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

        // if the current text would render beyond the bounds of the page,
        // start a new page and render it there instead
        if (size.height + currentPageY > maxHeight) {

            // create a new page and reset the current page's Y value
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
            currentPageY = kMarginInset;
        }

        // render the text
        [word drawInRect:CGRectMake(kMarginInset, currentPageY, maxWidth, maxHeight) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

        currentPageY += size.height;
        // increment the page number.

    }
    currentPage++;

    // end and save the PDF.
    UIGraphicsEndPDFContext();

}
jroyce
  • 2,029
  • 2
  • 22
  • 45
  • You only loop once. Why do you expect more than one page? – rmaddy Nov 20 '15 at 00:56
  • I'll add the attempts I made at looping and creating the additional pages. – jroyce Nov 20 '15 at 00:57
  • Don't set `done` to `YES` until you have added all of the pages you want. – rmaddy Nov 20 '15 at 01:24
  • But how do I know if I have added all the pages? Its based on the size of theText. – jroyce Nov 20 '15 at 01:27
  • You have to do lots of calculations to figure out how much text fits on each page taking into account the header, border, and image. – rmaddy Nov 20 '15 at 01:29
  • Right. Can you give me an example on how to go about that in the answer and I'll take it from there? – jroyce Nov 20 '15 at 01:30
  • I updated the post above to include the specific issue I can't resolve. How to loop through a text field by word or by sentence while checking the size.height of the pdf being created. – jroyce Nov 20 '15 at 19:18
  • 1
    It may help - https://stackoverflow.com/questions/58483933/create-pdf-with-multiple-pages – Pratik Sodha Oct 22 '19 at 13:50

0 Answers0