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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With