I want to Make PDF with Dynamic Content,like text will be dynamic,images will be dynamic so depends on content of course pages wil be dynamic as well,I have followed this tutorial http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265
but in this tutorial content is not dynamic.
You can pass value using javaScript like this for your dynamic value
Suppose you have a key "myKey" in your HTML
<td><span class="fieldVal" id="myKey"></span></td>
And Pass value using like this
NSBundle *bundle = [NSBundle mainBundle];
NSURL *indexFileURL = [bundle URLForResource:@"name_of_your_local_html" withExtension:@"html"];
[wbView loadRequest:[NSURLRequest requestWithURL:indexFileURL]];
[wbView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('myKey').innerHTML='%@'",@"myValue"]];
Now for converting HTML to PDF you can use this class
https://github.com/iclems/iOS-htmltopdf/blob/master/NDHTMLtoPDF.h
the easiest way to handle this, like any print job in Cocoa or Cocoa-Touch, is to first make a UIView (or UIViewController) subclass to draw/layout your data as you want it printed. You should do this even if you don't intend to ever show this view on the screen in your finished app. (you still might put it on the screen in development in order to get it looking how you want it.. ) A very simple approach for a model of unknown size might be a UITableViewController. Use Autolayout or the old UIViewAutoResizingMasks so that this view will cope with resizing gracefully. Of course if it is a UIViewController subclass then you could do it with interface builder too..
Once you're in this position drawing to a PDF is trivial
-(BOOL )writePDFtoPath:(NSString *)filePath{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
//note that because I've passed in CGRectZero for the size the context //comes in its default size
/*
default pdf..
8.5 X 11 inch
612 x 792
*/
//this is only 72dpi, but its the default. I have a trick where I zoom
// out before drawing to improve resolution:
NSInteger numberPages = 3; //work out how many pages you need
for (NSInteger page = 0 : page < numberPages : page ++ ){
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
CGFloat scaleFactor = 3.0;
CGContextSaveGState(pdfContext);
CGContextScaleCTM(pdfContext, 1.0/scaleFactor, 1.0/scaleFactor);
///
/// context size is now (612.0 * 3.0 , 792.0 * 3.0) , i.e. resolution 72.0 * 3.0 dpi..
[self.pdfGeneratingView setFrame: CGRectMake( 0.0, 0.0, 612.0*scaleFactor, 792.0*scaleFactor) ]; //let autolayout make it fit
//prepare your view for this page
[self.pdfGeneratingView.layer renderInContext:pdfContext];
CGContextRestoreGState(pdfContext);
}//page drawing loop
BOOL success = [pdfData writeToFile:filePath atomically:YES];
return success;
}
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