gpt4 book ai didi

ios - 如何在 objective-c 中从uiwebview保存pdf

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:11:14 26 4
gpt4 key购买 nike

我想从应用程序中的 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

提前致谢。

最佳答案

从 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)createPDF:(UIWebView *)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(@"PDF 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 获得许 cocoa 以在 Documentation archive 中找到.引用主题ID:2416,示例ID:28437。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com