0

In xcode, how would I convert a UIImage into a PDF File? Once I figure this out, I will send it through an email. But everything I've found while researching, it results as a blank file or gives an error saying it's damaged. How should I convert it?

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
DCAdams
  • 75
  • 2
  • 7
  • Try with this http://ipdfdev.com/2011/04/22/convert-an-image-to-pdf-on-the-iphone-and-ipad/ – Pushp Jun 28 '16 at 04:01

3 Answers3

2
-(void)createPdf:(NSImage*)image
{  
  PDFDocument *pdf = [[PDFDocument alloc] init];
  NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileName];
  PDFPage *page = [[PDFPage alloc] initWithImage:image];
  [pdf insertPage:page atIndex: [pdf pageCount]];
  [pdf writeToFile:path];
}

USE the above method as follow :

NSImage *image = [[NSImage alloc] initWithContentsOfFile:PATH_OF_YOUR_PNG_FILE];
[self createPdf:image];

PDFDocument Class conforms to NSObject. You can use this PDFDocument class in the PDFKit Framework.

Akash KR
  • 778
  • 3
  • 11
  • Am I suppose to import a class? UIDocument is trying to replace PDFDocument, which Xcode is giving me an error on. – DCAdams Jun 28 '16 at 16:36
0
 1. Download Source File From Step 4 
 2. Import into your Project
 3. Put This code into your action 


NSData *pdfData = [[NSData alloc] init]; 
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
pdfData = [PDFImageConverter convertImageToPDF:chosenImage];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
4. Enjoy PDFImageConverter
Pushp
  • 1,064
  • 10
  • 18
0

First you need to create a PDF graphics context that is GraphicsBegin and GraphicsEndPDFContext. Your image from current view will capture and it will set on your PDF page.

Steps:

First call this method on any of your button tap event:

NSString *fileName = [NSString stringWithFormat:@“pdf file name here”];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

// This method will generate pdf file of your image. You can send that pdf file after this method.

[self generatePdfWithFilePath: pdfFileName];

Then set these methods in your view controller:

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 500, 810), nil);
    [self drawImage:@“page number of pdf file”];

    UIGraphicsEndPDFContext();
}

- (void) drawImage:(NSInteger)pageNumber
{
    UIImage * demoImage1;
    demoImage1 = [self captureView:@“Your image view”];
    [demoImage1 drawInRect:CGRectMake(0, 0, 500, 810)];
}

- (UIImage *)captureView:(UIView *)view {

    CGRect screenRect = view.frame;

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91