如何在iOS中从网站创建MHT或PDF文件



我有一个网站的URL,想将其内容保存为MHT文件,如果不可能,可以保存为PDF文件。我发现了一个不免费的图书馆(1年289美元)

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

我知道safari不支持MHT文件,所以这似乎是不可能的。。Safari支持webarcive格式,但我们的服务器不支持。对我来说,只有两个选项可以将网络内容保存为mht或pdf。

我找到了一个obj-C代码来将网络内容保存为图像。。

ios将网页保存为pdf

但我不想使用这种方式(将其保存为图像->将其转换为pdf)

用于从website创建MHT,如下所示:

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");

加载UIWebView后,从website创建PDF,如下所示:

UIWebView使用UIPrintPageRenderer按照以下步骤:

添加UIPrintPageRendererCategory以获取PDF数据

@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

添加A4尺寸的定义

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

现在在UIWebView's webViewDidFinishLoad delegate中使用UIWebViewUIPrintPageRenderer property

- (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");
  }
}

相关内容

  • 没有找到相关文章

最新更新