Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i make Pdf from UIImage?

Tags:

ios

pdf

uiimage

I have one UIImage in my UIViewController and I want to migrate that image to PDF formate so is it possible to such kind of migration?Please guide me i don't have idea of this.

like image 866
Ankit Vyas Avatar asked Jan 12 '12 15:01

Ankit Vyas


People also ask

How do I create a PDF from images?

Learn how to convert image files to PDF online, including JPG, PNG, BMP, GIF, or TIFF files: Click the Select a file button above or drag and drop a file into the drop zone. Select the image file you want to convert to PDF. After uploading, Acrobat automatically converts the file from an image format to PDF.

How do I make an image a PDF in Linux?

The first option on our list is to use gscan2pdf app. Gscan2pdf is an open-source app available on all Linux distros and makes converting images to PDF a breeze. We can install it by going to the software centre and searching for gscan2pdf . After opening the app, click on a folder icon to load images.


2 Answers

If you have UIImageView into UIViewController then call this method:

-(NSData*)makePDFfromView:(UIView*)view
{
    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData(pdfData, view.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:pdfContext];
    UIGraphicsEndPDFContext();

    return pdfData;
}

Use like this:

NSData *pdfData = [self makePDFfromView:imgView];
//[pdfData writeToFile:@"myPdf.pdf" atomically:YES]; - save it to a file
like image 132
beryllium Avatar answered Oct 20 '22 20:10

beryllium


It's not very hard, but there are a few steps to set everything up. Basically, you need to create a PDF graphics context, and then draw into it with standard drawing commands. To simply put an UIImage into a PDF, you could do something like the following:

// assume this exists and is in some writable place, like Documents
NSString* pdfFilename = /* some pathname */

// Create the PDF context
UIGraphicsBeginPDFContextToFile(pdfFilename, CGRectZero, nil); // default page size
UIGraphicsBeginPDFPageWithInfo(CGRectZero, nil);

// Draw the UIImage -- I think PDF contexts are flipped, so you may have to
// set a transform -- see the documentation link below if your image draws
// upside down
[theImage drawAtPoint:CGPointZero];

// Ending the context will automatically save the PDF file to the filename
UIGraphicsEndPDFContext();

For more information, see the Drawing and Printing Guide for iOS.

like image 27
Jason Coco Avatar answered Oct 20 '22 20:10

Jason Coco