iphone - 将 HTML 转换为 PDF 时出现额外空白页

标签 iphone ios objective-c

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

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

现在问题是在创建的 PDF 中添加空白页

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

最佳答案

相应更改:

首先A4尺寸是这样的:

#define kPaperSizeA4 CGSizeMake(595,842)

更改为:

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

现在PDF内容页边距增加:

printableRect = CGRectMake(10, 18, kPaperSizeA4.width-20, kPaperSizeA4.height-36);

更多请引用Avoid extra blank page (with A4, in some unexpected cases

关于iphone - 将 HTML 转换为 PDF 时出现额外空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18426817/

相关文章:

ios - 关闭模态窗口将父级切换为横向

ios - 检查 View 是否在其他 View 中?

ios - Unity3D导出源: c++ header files not recognised

Objective-c - 在静态 NSMutableDictionary 上使用 NSCache 的好处?

iphone - 在 iPad 上自动调整大小

javascript - 触摸事件在 IOS 7 Iphone 中不起作用

iphone - 重定向 UIAlertView 按钮推送通知

ios - iPhone 和 Ipad 崩溃 :reason: '*** setObjectForKey: object cannot be nil (key: objectid)'

objective-c - Objective C - 数字数组

ios - 使用 SDWebImage 处理超大图像的下载