I load 10 page pdf file in uiwebview in objective c. After I want to write new pdf file from uiwbview. Please any one help to answer
i want to write UIWebview.scrollView. Because i have added some image in UIWebview scrollview subview . its possible
Please check my below code im getting 10 pages in single pdf page. I dont know what the issues ?
-(void)createPDFfromUIView:(UIWebView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
NSArray *viewsToRemove1 = [self.pdfView.scrollView subviews];
for (UIView *v1 in viewsToRemove1)
{
int imgL = v1.frame.size.width;
if (imgL < 75 && imgL > 67)
{
[v1 removeFromSuperview];
aView = pdfView;
}
}
CGSize fullSize = aView.scrollView.contentSize;
NSMutableData *pdfData = [NSMutableData data];
CGRect oldFrame = aView.frame;
[aView sizeToFit];
CGRect rect = CGRectMake(aView.scrollView.contentOffset.x, aView.scrollView.contentOffset.y, fullSize.width, fullSize.height);
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, self.pdfView.scrollView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[self.pdfView.scrollView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
[pdfData writeToFile:getPdfpath atomically:YES];
NSLog(#"documentDirectoryFileName: %#",getPdfpath);
}
You can use something like this to load the PDF into an NSData object:
NSData *pdfFile = [NSData dataWithContentsOfURL:webView.request.URL];
Then, you can use the NSData methods to write the data to a file:
[pdfFile writeToFile:filePath atomically:YES];
Of course, you'll need to setup filePath to be an appropriate writable location to store the file/
If you need to call and access any file then first of all you need to add that file in your project after that you can use the below code to access it.
NSString* path = [[NSBundle mainBundle] pathForResource:#"Terms"
ofType:#"txt"];
NSString *content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
Related
when I add some text in the textview, onclick of convert to pdf,then i have to convert textview text to pdf file.
can anyone give some solution for this problem.
Thanks for quick response
Refer this code.
NSString *text = #"this is textField's text";
NSMutableData *pdfData = [[NSMutableData alloc] init];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, 320, 480), nil);
UIGraphicsBeginPDFPage();
[text drawInRect:UIGraphicsGetPDFContextBounds() withAttributes:#{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]}];
UIGraphicsEndPDFContext();
NSString *file = #"file.pdf"; //this is the file name.
NSArray<NSURL *> *dir = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
if (dir.count > 0){
NSURL *path = [[dir firstObject] URLByAppendingPathComponent:file];
[pdfData writeToFile:path.path atomically:false];
}
You can provide any formatting options you want in the withAttributes dictionary in the draw method.
And if you want multiple pages in your PDF just put this code in a loop
UIGraphicsBeginPDFPage();
[text drawInRect:UIGraphicsGetPDFContextBounds() withAttributes:#{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]}];
UIGraphicsEndPDFContext();
and change the text that draws. You can draw images too if you want them in your PDF.
Check the created PDF
Hope this helps.
I'm trying to create a PDF from a UIView and works well, as "How to Convert UIView to PDF within iOS?" did.
But my iPad is landscape and is a form, lengthy form, with height 3k.
When I convert to PDF, it converts only the size of the height of the iPad, namely 748 and not 3k I need.
What is wrong? The iOS version?
EDITED: Heres the code like #wain ask
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView drawRect:aView.bounds];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
}
I find the solution from here "UIGraphicsGetCurrentContext and UIScrollView" and adaptate to my code and now grab all form and print a nice PDF.
If someone need my code, please let me know and i post the code.
I'm trying to create a new PDF with custom font.
I load my custom font and I can use it in all app, but when I tried to use it creating my PDF, the System font is printed.
I'm using NSString UIKit Additions.
+(UIFont *)imagoBook:(float)size{
return [UIFont fontWithName:#"Imago-Book" size:size];
}
-(NSData *)createPDFfromUIView
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.bounds, nil);
UIGraphicsBeginPDFPage();
UIFont *imagoBook13 = [ROCTextsInformation imagoBook:13];
[#"Test" drawAtPoint:CGPointZero withFont:imagoBook13];
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:#"t.pdf"];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
return pdfData;
}
In fact I'm drawing the same context in a view and the font is written perfectly but not in the pdf.
I'm using otf files for the font.
Thankssss
Finally I have solved the problem converting the file .otf to .ttf, and it works!
I need to create a multipage PDF with signature (UIImage) at the bottom. I would also like to have a line created so that the signature could be on a line. Here's the code I have so far:
- (IBAction)saveAsPDF:(id)sender
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, 612, 792), nil);
UIGraphicsBeginPDFPage();
[nameTextField.text drawInRect:CGRectMake(50, 40, 512, 30) withFont:[UIFont systemFontOfSize:16] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
[TextView.text drawInRect:CGRectMake(50, 90, 512, 672) withFont:[UIFont systemFontOfSize:12]];
[SignatureImageView.image drawInRect:CGRectMake(30, 500, 190, 71)];
UIGraphicsEndPDFContext();
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentNameWithExtention = [NSString stringWithFormat:#"%#.pdf", nameTextField.text];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:documentNameWithExtention];
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
So, I need to know how to make it multipage and I need to know how to add the signature at the end of the body text.
Any help would be appreciated.
From the docs
The functions you use to create a PDF graphics context allow you to
specify a default page size but they do not automatically open a page.
After creating your context, you must explicitly open a new page using
either the UIGraphicsBeginPDFPage or UIGraphicsBeginPDFPageWithInfo
function. And each time you want to create a new page, you must call
one of these functions again to mark the start of the new page. The
UIGraphicsBeginPDFPage function creates a page using the default size,
while the UIGraphicsBeginPDFPageWithInfo function lets you customize
the page size and other page attributes.
I'm currently creating and saving a PDF of a UIView. It is working great, but now I'm trying to create a PDF from text data saved to the disk rather than from the UIView. Ideally, I would like to create a PDF based on the text in the UITextView. The below code writes the first part of the text view just fine, but it doesn't create the PDF from the entire text view. It is quite a bit of text, so it only created the PDF from the amount of text that would fit on the view. The code that I'm currently using is this:
- (void)createPDFfromUIView:(UITextView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{
NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, textview.bounds, myDictionary);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[textview.layer renderInContext:pdfContext]; // this line
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:#"filename"];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
And to call it / save it, I put this message send in an IBAction: [self createPDFfromUIView:textview saveToDocumentsWithFileName:#"pdflocation"];
Now, I've been parsing through this code and the docs for hours, and cannot figure out how to create a PDF based upon a saved text file rather than the view. I'm still new to programming, so a lot of this is going over my head. Does anyone know how ot change the above code to have it create a PDF based upon saved text rather than the view?
Options: I could create a PDF based on output of an NSString. I could also throw the saved text into a UITextView and create the PDF based upon that (original plan) I however don't know how to do either.
This article from Apple explains how to create Pdf files and draw text on Pdf pages.
For those still looking for some sample code, this works:
- (NSData *)createPDFforText: (NSString *)text
{
NSMutableData *pdfData = [NSMutableData data];
NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
UIGraphicsBeginPDFPage();
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
[attributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
[attributes setObject: [UIFont fontWithName: LATO_REGULAR size: 20.f] forKey: NSFontAttributeName];
[attributes setObject: [UIColor blackColor] forKey: NSForegroundColorAttributeName];
[text drawAtPoint: CGPointMake(20.f, 44.f) withAttributes: attributes];
UIGraphicsEndPDFContext();
return pdfData;
}
The CGRectZero bounds value sets the PDF document to the default page size of 8.5 by 11 inches (612 by 792 points).