Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a pdf from two UIViews with 2 pages

I have two UIViews that needs to be converted to a PDF and send as an attachment. My code below overlaps the first UIView. I would like to send this as an attachment to an email.

Thank you.

NSMutableData *pdfData=[NSMutableData data];


UIGraphicsBeginPDFContextToData(pdfData,aView.bounds, nil);
CGContextRef pdfContext=UIGraphicsGetCurrentContext();

UIGraphicsBeginPDFPage();
[aView.layer renderInContext:pdfContext];
 UIGraphicsEndPDFContext();


UIGraphicsBeginPDFContextToData(pdfData, bView.bounds, nil);

CGContextRef pdfContext2=UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();

[bView.layer renderInContext:pdfContext2];
  UIGraphicsEndPDFContext();



MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
NSString *subject=[NSString stringWithFormat:@"Delivery Report-%@",[globUserID uppercaseString]];
[mailer setSubject:subject];
[mailer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"DeliveryReport.pdf"];
[self presentModalViewController:mailer animated:YES];
like image 275
baste Avatar asked Dec 16 '22 11:12

baste


1 Answers

Don't create a second context.

NSMutableData *pdfData=[NSMutableData data];
CGRect bounds = CGRectMake(0, 0, 612, 792); // letter sized paper, adjust as needed

UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();

UIGraphicsBeginPDFPage();
[aView.layer renderInContext:pdfContext];

UIGraphicsBeginPDFPage();
[bView.layer renderInContext:pdfContext];

UIGraphicsEndPDFContext();
like image 71
rmaddy Avatar answered Dec 26 '22 12:12

rmaddy