1

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

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

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

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

ios将网页另存为pdf

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

4

1 回答 1

2

MHTwebsite这样创建:

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

像这样加载PDF后创建:websiteUIWebView

按照以下步骤使用UIPrintPageRendererUIWebView

添加Category用于UIPrintPageRenderer获取 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使用UIPrintPageRenderer property.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");
  }
}
于 2015-04-08T15:18:33.650 回答