23 Apr 2009, 17:37
Generic-user-small

Kyle Smith (8 posts)

Does anyone know if it’s possible to create a PDF from a UIView. Something similar to NSView’s dataWithPDFInsideRect method? I haven’t been able to find any information on it, although I know of a few applications that do just that. Unless of course they’re forwarding a request to their server, which handles it, then sends back a PDF. Thanks.

24 Apr 2009, 13:08
Biopic_100x100_pragsmall

Bill Dudney (917 posts)

Hi Kyle,

Since the printing stuff has not been moved to iPhone I doubt it. You could get a png from your view fairly easily, not sure how difficult it would then be to create a PDF with that image.

Good luck!

24 Apr 2009, 18:57
Generic-user-small

Kyle Smith (8 posts)

That would probably work for me. Do you know how it’s done? I’m not seeing anything in the UIView’s documentation.

I was hoping for some finer detail than what the iPhone screen can display, but depending on how it’s accomplished, I might be able to render a larger UIView off screen and dump that to a PNG.

Thanks for the help!

24 Apr 2009, 20:41
Biopic_100x100_pragsmall

Bill Dudney (917 posts)

My PDF Fu is weak…

From what I remember though you can build simple PDF from scratch with the Quartz apis…

Good luck!

25 Apr 2009, 12:56
Generic-user-small

Kyle Smith (8 posts)

Oops, I guess I should have been more specific. Do you know how to create a PNG from a UIView? I haven’t been able to find any info on it. I did see a reference to an undocumented, unofficial _writeApplicationSnapshot, but I’m not sure it’ll suit my needs. Thanks.

Once I figure out how to turn that PNG to a PDF, I’ll be sure to post the results up here.

25 Apr 2009, 13:40
Generic-user-small

Kyle Smith (8 posts)

It looks like the UIImage class has a nifty imageWithData method. Now if I can only get a NSDate representation of a UIView.

25 Apr 2009, 13:56
Biopic_100x100_pragsmall

Bill Dudney (917 posts)

Instead create a bitmap graphics context, make it current. get the views layer tell the layer to drawInContext:

Then you can get the UIImage or CGImageRef from the context.

Good luck!

25 Apr 2009, 21:54
Generic-user-small

Kyle Smith (8 posts)

I couldn’t get the bitmap context to work, mostly because I haven’t done much work with contexts in the past. However, while researching the in’s and out’s of contexts, I found this nugget:

http://developer.apple.com/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html#//apple_ref /doc/uid/TP30001066-CH214-TPXREF101

Which shows you the basic code for creating a PDF. Perfect!

25 May 2011, 19:59
Generic-user-small

Bill Shirley (1 post)

here’s some simple code to “print out” a UIView into a PDF - the resolution unfortunately is screen resolution

#import <QuartzCore/CALayer.h>

- (NSString *)pdfPath {
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];
  return writableDBPath;
}

- (NSDictionary *)pdfContextDictionary {
  NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:@"J. P. Author", kCGPDFContextAuthor,
                     @"My Cool App", kCGPDFContextCreator,
                     @"This is the Output", kCGPDFContextTitle,
                     nil];
  return d;
}

- (void)printToPDF {
  NSString *path = [self pdfPath];
  UIView *displayView = self.view;
  CGRect pageRect = displayView.bounds;
  if (UIGraphicsBeginPDFContextToFile(path, pageRect, [self pdfContextDictionary]) == NO) {
    return; // error
  }
  UIGraphicsBeginPDFPage();
  CGContextRef context = UIGraphicsGetCurrentContext();
  CALayer *layer = displayView.layer;
  [layer renderInContext:context];
  UIGraphicsEndPDFContext();  
  NSLog(@"[%@ %@] %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), path);
}
  You must be logged in to comment