gpt4 book ai didi

ios - 如何在不使用临时文件的情况下在 UIWebview 中打开受密码保护的 PDF/DOC?

转载 作者:行者123 更新时间:2023-11-29 12:01:46 25 4
gpt4 key购买 nike

我们正在尝试在 UIWebview 中打开受密码保护的 PDF/DOC 文件。出于安全考虑,我们只能从服务器检索文件并存储到内存中(即 NSData),而不能存储为文件。

我们尝试在UIWebiew中使用loaddata函数,它只能在没有密码保护的情况下加载doc/xls/ppt/pdf。

对于带密码的doc/xls文件,显示“Unable to Read Document. The operation couldn't be completed (QuickLookErrorDomain error 44820/912)。请问如何在UIWebview中打开文件?

对于带密码的pdf文件,它显示“文档“空白”受密码保护”。请问如何才能显示正确的文档名称而不是“空白”?

我也搜索了 cgpdf 并知道如何将 pdf 文件解锁为 CGPDFDocumentRef,但找不到将其改回 NSData 并直接输入到 UIWebview 的方法。

谢谢。

最佳答案

在本地写入文件并读取数据并在 WebView 中显示

//convert file data(ie : NSData) as string
NSData *fileData;//This is the data from your request
NSString *fileString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];

//get the file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"sample.pdf"];

//Save as file
NSError *error;
BOOL hasFileWritten = [fileString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

if(!hasFileWritten)
{
NSLog(@"Write file error: %@", error);
}

//open pdf in webview
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

更新不在本地写入文件数据,在内存中处理数据并在webview中显示pdf文件

@interface ViewController ()
{
IBOutlet UIWebView *webView;
CATiledLayer *tiledLayer;
CGPDFPageRef pageRef;
}
@end


CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url);

BOOL success = CGPDFDocumentUnlockWithPassword(pdf, "test");

if(success)
{
pageRef = CGPDFDocumentGetPage(pdf, 1);

CGRect pageRect = self.view.frame;

tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
tiledLayer.levelsOfDetail = 1000;
tiledLayer.levelsOfDetailBias = 1000;
tiledLayer.frame = pageRect;

UIView *contentView = [[UIView alloc] initWithFrame:pageRect];
[contentView.layer addSublayer:tiledLayer];

[webView addSubview:contentView];
}


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, pageRef);
}

关于ios - 如何在不使用临时文件的情况下在 UIWebview 中打开受密码保护的 PDF/DOC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687901/

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