将HTML转换为PDF时的额外空白页面

时间:2013-08-25 07:48:13

标签: iphone ios objective-c

我已经PDF创建UIWebView,如下所示:

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];

[render addPrintFormatter:_webView.viewPrintFormatter startingAtPageAtIndex:0];

CGRect printableRect;
CGRect paperRect;
printableRect = CGRectMake(10, 5, kPaperSizeA4.width-20, kPaperSizeA4.height-10);
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) {
     BOOL isSuccess = [pdfData writeToFile:aStrDocDirPath atomically:YES];
     if (isSuccess)
     {
         NSLog(@"Created and Saved");
     }
}

现在问题是在blank page中添加created PDF

我提到Extra blank page when converting HTML to PDF using abcPDF。但在iOS我们怎样才能摆脱它?

2 个答案:

答案 0 :(得分:4)

相应更改:

首先A4 size是这样的:

#define kPaperSizeA4 CGSizeMake(595,842)

将其更改为:

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

现在increased page margin content PDF中的printableRect = CGRectMake(10, 18, kPaperSizeA4.width-20, kPaperSizeA4.height-36);

{{1}}

请参阅更多Avoid extra blank page (with A4, in some unexpected cases

答案 1 :(得分:0)

将HTML转换为PDF时,我还遇到了额外的空白首页问题。我正在使用FlyingSaucer进行HTML到PDF的转换。

我的HTML上有这个CSS定义:

@page Section1{
  size: 612.45pt 841.7pt;
}

div.Section1 {
  page: Section1;
}

当我将CSS定义修改为:

时,删除了额外的页面
@page {
      size: 612.45pt 841.7pt;
}