1

I have a website's URL and want to save its content as MHT file, if not possible then can be as a PDF file. I found a library which is not free ( for 1 year $289)

http://www.chilkatsoft.com/mht-features.asp

I know that safari doesnt support MHT file then it seems impossible.. Safari suppors webarcive format but our server doesnt support it. Then for me there are just 2 options for saving webcontent as mht or pdf.

I found an obj-C code to save webcontent as an image..

ios save web page as pdf

but i dont want to use this way (save it as an image->convert it as a pdf)

Community
  • 1
  • 1
ertan2002
  • 1,458
  • 1
  • 32
  • 68

1 Answers1

2

For creating MHT from website like this:

NSString *googleString = @"http://www.google.com";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL
                                                encoding:NSASCIIStringEncoding
                                                   error:&error];

NSData *data = [googlePage dataUsingEncoding:NSASCIIStringEncoding];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
documentsPath = [documentsPath stringByAppendingPathComponent:@"test.mht"];
BOOL isSucess = [data writeToFile:documentsPath atomically:YES];
if (isSucess)
    NSLog(@"written");
else
    NSLog(@"not written");

Create PDF from website after loading UIWebView like this :

Use UIPrintPageRenderer from UIWebView Follow below steps :

Add Category of UIPrintPageRenderer for getting PDF Data

@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end

@implementation UIPrintPageRenderer (PDF)
- (NSData*) printToPDF
{
  NSMutableData *pdfData = [NSMutableData data];
  UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
  [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
  CGRect bounds = UIGraphicsGetPDFContextBounds();
  for ( int i = 0 ; i < self.numberOfPages ; i++ )
  {
    UIGraphicsBeginPDFPage();
    [self drawPageAtIndex: i inRect: bounds];
  }
  UIGraphicsEndPDFContext();
  return pdfData;
}
@end

Add these define for A4 size

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

Now in UIWebView's webViewDidFinishLoad delegate use UIPrintPageRenderer property of UIWebView.

- (void)webViewDidFinishLoad:(UIWebView *)awebView
{
  if (awebView.isLoading)
    return;

  UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
  [render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];
  //increase these values according to your requirement
  float topPadding = 10.0f;
  float bottomPadding = 10.0f;
  float leftPadding = 10.0f;
  float rightPadding = 10.0f;
  CGRect printableRect = CGRectMake(leftPadding,
                                  topPadding,
                                  kPaperSizeA4.width-leftPadding-rightPadding,
                                  kPaperSizeA4.height-topPadding-bottomPadding);
  CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
  [render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
  [render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
  NSData *pdfData = [render printToPDF];
  if (pdfData) {
    [pdfData writeToFile:[NSString stringWithFormat:@"%@/tmp.pdf",NSTemporaryDirectory()] atomically: YES];
  }
  else
  {
    NSLog(@"PDF couldnot be created");
  }
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • thank you very much, I use Xamarin.iOS so i must re-write code as C# if it exactly works? – ertan2002 Apr 08 '15 at 15:29
  • secondly, it creates just one page? i mean how i can split according to long? if website is like 5 pages, i can create 5 pages pdf with that code? I think that it just generate pdf as 1 page? – ertan2002 Apr 08 '15 at 15:30