5

I am drawing a PDF file and cannot figure out how to embed the font into the PDF itself, could anyone help me out? It displays fine on the device however once it is emailed out or printed via AirPrint I just get the fallback font. Here is my code...

- (void) drawChart{
CGContextRef    currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);


NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *pChart1 = [data stringForKey:@"pchart1"];


NSString *textToDraw = [NSString stringWithFormat:@"%@",pChart1];

UIFont *font = [UIFont fontWithName:@"MyFont" size:14];

CGSize stringSize = [textToDraw sizeWithFont:font
                           constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
                               lineBreakMode:UILineBreakModeWordWrap];

CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

[textToDraw drawInRect:renderingRect 
              withFont:font
         lineBreakMode:UILineBreakModeWordWrap
             alignment:UITextAlignmentLeft];
}

Any help would be much appreciated!

Dan
  • 398
  • 4
  • 15
  • I have searched hi and low for days and have come to the conclusion that this may not be possible so for now Iam just getting an image of the displayed are like this `UIGraphicsBeginImageContext(self.myChart.bounds.size); [self.myChart.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData * chartdata = UIImagePNGRepresentation(image);` – Dan Jan 25 '12 at 04:55
  • I would also really like to see a solution for this - I am running into the exact same requirement. I have added a bounty to try and get more attention to this problem. – Tom Jul 06 '12 at 14:07
  • I have a similar issue, and I think I have solved it, but I won't know until I can try it out. Just to make sure, are you using OpenType Fonts? – Tom Jul 06 '12 at 16:44
  • 1
    if there's no API for this, seems like you could generate the PDF data from your PDF context+data consumer, and then embed the font yourself based on the PDF 1.7 spec... – nielsbot Jul 12 '12 at 00:21

2 Answers2

5

I have no idea why, but, on an application I was involved in we had this problem, once we switched from using an open type font to true type font it just worked.

palumner
  • 51
  • 1
  • 4
1

As an update: prior to iOS 8, fonts need to be TTF to be embedded in a pdf, but iOS8+ supports OTF font embedding. No special magic is required.

Wil Macaulay
  • 507
  • 3
  • 12
  • Thanks! By chance, can you suggest some reference to your statement? (i.e. Apple documentation) – Lubbo Jan 25 '16 at 11:22
  • No reference, but from experimentation (I had an OTF font prior in iOS 7, had to use an online converter to make it work. Tried it again on iOS 8, worked fine. See http://appstore.com/thecraic - the PDF files it creates use an OTF font (Bravura). – Wil Macaulay Feb 11 '16 at 18:34