I want save my image in PDF format in document directory. Please suggest me for saving format . I know only PNG and JPEG saving format in iOS .
-
1Try the following two options: - http://stackoverflow.com/a/2445451/942966 - http://stackoverflow.com/a/3880883/942966 – Roshit May 28 '13 at 06:40
2 Answers
First we compute the size of the PDF page. Based on the image size and the resolution we want to draw the image, the page size is computed like this (for flexibility different horizontal and vertical resolutions are supported):
double pageWidth = image.size.width * image.scale * 72 / horzRes;
double pageHeight = image.size.height * image.scale * 72 / vertRes;
Now we're ready to create the PDF file. It can be created directly on disk or in memory. For flexibility let's create it in memory.
NSMutableData *pdfFile = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer =
CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfFile);
// The page size matches the image, no white borders.
CGRect mediaBox = CGRectMake(0, 0, pageWidth, pageHeight);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &mediaBox, NULL);
The PDF context is created, we can now perform the conversion:
CGContextBeginPage(pdfContext, &mediaBox);
CGContextDrawImage(pdfContext, mediaBox, [image CGImage]);
CGContextEndPage(pdfContext);
The work is done, finalize the PDF file and release the used objects.
CGContextRelease(pdfContext); CGDataConsumerRelease(pdfConsumer);
The PDF file is now available in the pdfFile variable. I placed all the code above in the convertImageToPDF:withHorizontalResolution:verticalResolution: static method in the PDFImageConverter utility class, it can be used like this:
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withHorizontalResolution: 300 verticalResolution: 300];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
Sometimes it is necessary to convert an image to PDF and use a predefined page size, such as Letter or A4. Also a maximum bounding rectangle can be specified for the image, so it is placed at a specific position on the page and it does not get out of the page. In this situation the image size must be adjusted (increased resolution) to make sure the image fits the given rectangle.
double imageWidth = image.size.width * image.scale * 72 / resolution;
double imageHeight = image.size.height * image.scale * 72 / resolution;
double sx = imageWidth / boundsRect.size.width;
double sy = imageHeight / boundsRect.size.height;
// At least one image edge is larger than maxBoundsRect, reduce its size.
if ((sx > 1) || (sy > 1)) {
double maxScale = sx > sy ? sx : sy;
imageWidth = imageWidth / maxScale;
imageHeight = imageHeight / maxScale;
}
// Put the image in the top left corner of the bounding rectangle
CGRect imageBox = CGRectMake(
boundsRect.origin.x, boundsRect.origin.y + boundsRect.size.height - imageHeight,
imageWidth, imageHeight);
Having the final box, the image can be converted to PDF using the code in the first half of the article. The code for the new conversion method is placed in the convertImageToPDF:withResolution:maxBoundsRect:pageSize: static method in the PDFImageConverter utility class, it is used like this:
CGSize pageSize = CGSizeMake(612, 792);
CGRect imageBoundsRect = CGRectMake(50, 50, 512, 692);
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withResolution: 300 maxBoundsRect: imageBoundsRect pageSize: pageSize];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
Also you can download the source code form Here.

- 16,776
- 8
- 50
- 72
You can use this tutorial convert-an-image-to-pdf-on-the-iphone-and-ipad
Then save pdf file in document directory using :
//******** Method To Copy Image to Directory ******** //
- (void) copyFileToDocuments:(NSString *)fileURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/convertedImage.pdf"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:fileURL] toURL:[NSURL fileURLWithPath:destinationPath] error:&error];
}
then call
[self copyFileToDocuments:moviePath];
Hope it helps you.

- 9,893
- 3
- 40
- 61