OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 objective-c 中从uiwebview保存pdf [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:30
标题: ios - 如何在 objective-c 中从uiwebview保存pdf

我想在应用程序中从 UIWebView 将 pdf 保存到 ibook,UIWebView 中的 html 可以是多页的。
我尝试使用

NSString *page = [self.lowWebview stringByEvaluatingJavaScriptFromString"document.body.outerHTML"];
NSString *path = [self exportHTMLContentToPDF:page];

NSURL *targetURL = [NSURL fileURLWithPath:path];

self.docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString"itms-books:"]])
{
    [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    NSLog(@"iBooks installed");

} else {

    NSLog(@"iBooks not installed");

}

导出HTMLContentToPDF:

CustomPrintPageRenderer *printPageRenderer = [[CustomPrintPageRenderer alloc] init];

UIMarkupTextPrintFormatter *printFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:HTMLContent];

[printPageRenderer addPrintFormatter:printFormatter startingAtPageAtIndex:0];//.addPrintFormatter(printFormatter, startingAtPageAtIndex: 0)

NSData *pdfData = [self drawPDFUsingPrintPageRenderer:printPageRenderer];

NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [docDirPath stringByAppendingPathComponent"ticket.pdf"];

[pdfData writeToFile:fileName atomically:true];//.writeToFile(fileName, atomically: true)

NSLog(@"%@", fileName);
return fileName;

问题是 exportHTMLContentToPDF 如果 UIWebView 包含多个页面,则只创建一个页面,并且有时输出格式不能很好地打印在 pdf 中

提前致谢。



Best Answer-推荐答案


从 UIWebview 中加载的任何 Microsoft 文档创建 PDF

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

首先实现 UIPrintPageRenderer 协议(protocol)

@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

然后,在 UIWebView

中文档加载完成后调用下面的方法
-(void)createPDFUIWebView *)webView {

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];

float padding = 10.0f;
CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
CGRect printableRect = CGRectMake(padding, padding, kPaperSizeA4.width-(padding * 2), kPaperSizeA4.height-(padding * 2));

[render setValue:[NSValue valueWithCGRect:paperRect] forKey"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey"printableRect"];

NSData *pdfData = [render printToPDF];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    if (pdfData) {
        [pdfData writeToFile:directoryPath atomically: YES];
    }
    else
    {
        NSLog(@"DF couldnot be created");
    }
});}

摘自 PDF Creation in iOS - Create PDF from any Microsoft Document loaded in UIWebview .原作者是Mansi Panchal .归属细节可以在 contributor page 上找到。 .源在 CC BY-SA 3.0 下获得许可并且可以在 Documentation archive 中找到.引用主题 ID:2416 和示例 ID:28437。

关于ios - 如何在 objective-c 中从uiwebview保存pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43446153/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4