Raju
Raju

Reputation: 451

Get Print preview and save it in PDF file

I want to generate a print preview of a web page and save it to an PDF file.

i tried stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"; and

stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"; 

functions for finding the height and width but problem with this is the java script program stops after 10 second..

I want to use default print option that is used for airPrint, but i don't want that to print.

I just want to save the print preview in PDF.

Please any one help me..

Upvotes: 1

Views: 582

Answers (2)

Tushar
Tushar

Reputation: 88

NSString *webServiceUrlString =@"https://www.google.com"; //Google.com is for example use your url
NSData *pdfData = [[NSData  alloc]initWithContentsOfURL:[NSURL URLWithString:webServiceUrlString]];

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSFileManager *fm = [NSFileManager defaultManager];

NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:resourceDocPath error:&error]) {
    BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@", resourceDocPath, file] error:&error];
    if (!success || error) {

    }
}

NSString* fileName = @"Your File Name.pdf";
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:fileName];

[pdfData writeToFile:filePath atomically:YES];

Upvotes: 1

Mudit Bajpai
Mudit Bajpai

Reputation: 3020

You can use UIGraphicsBeginPDFContextToFile to create a PDF file from your webpage content.

Check out the following tutorial.MAKING A PDF FROM A UIWEBVIEW

Upvotes: 0

Related Questions