如何刷新NSDocumentDirectory中的HTML文件? (不止一次)

时间:2013-03-14 11:08:47

标签: objective-c pdf-generation

我在 DocumentDirectory 中使用 HTML 文件生成pdf 文件。每次我需要刷新 (在html文件中具有相同名称的新图像) HTML文件在以下方法调用时。

现在我正在这样做,但它只是第一次刷新(当我调用这种方法时)

-(void)RefreshingHTML
{
// fetching path
NSArray* deletepath_forDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString* deletedocumentsDirectoryfiles = [deletepath_forDirectory objectAtIndex:0];`

// Delete HTML file in DocumentDirectory
NSString *deleteHTMLPath = [deletedocumentsDirectoryfiles stringByAppendingPathComponent:@"HTML_Demo.html"];
NSString *deletePDFPath = [deletedocumentsDirectoryfiles stringByAppendingPathComponent:@"HTML_Demo.pdf"];
NSLog(@"delete HTML file Path : %@",deleteHTMLPath);
if([[NSFileManager defaultManager] fileExistsAtPath:deleteHTMLPath]) {

    [[NSFileManager defaultManager] removeItemAtPath:deleteHTMLPath error:NULL];
    [[NSFileManager defaultManager] removeItemAtPath:deletePDFPath error:NULL];
}
// fetching HTML file from supporting files
NSString *path = [[NSBundle mainBundle] pathForResource:@"HTML_Demo" ofType:@"html"];
NSURL *pathURL = [NSURL fileURLWithPath:path];
NSArray* path_forDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* documentsDirectory = [path_forDirectory objectAtIndex:0];

// saving HTML file to DocumentDirectory
NSData* data = [NSData dataWithContentsOfURL:pathURL];
[data writeToFile:[NSString stringWithFormat:@"%@/HTML_Demo.html",documentsDirectory] atomically:YES];

// Fetching HTML file from DocumentDirectory
NSString *HTMLPath = [documentsDirectory stringByAppendingPathComponent:@"HTML_Demo.html"];
NSLog(@"delete HTML file Path: %@",HTMLPath);
if([[NSFileManager defaultManager] fileExistsAtPath:HTMLPath]) {

    NSURL *targetURL = [NSURL fileURLWithPath:HTMLPath];

    // Converting HTML to PDF
    self.PDFCreator = [NDHTMLtoPDF createPDFWithURL:targetURL
                                     pathForPDF:[@"~/Documents/HTML_Demo.pdf" stringByExpandingTildeInPath]
                                       delegate:self
                                       pageSize:kPaperSizeA4
                                        margins:UIEdgeInsetsMake(20, 5, 90, 5)];
}


}

1 个答案:

答案 0 :(得分:1)

可能正在从缓存加载URL。在NDHTMLtoPDF.m中,查看viewDidLoad:

而不是:[webview loadRequest:[NSURLRequest requestWithURL:self.URL]];

尝试使用允许您设置cachePolicy的构造函数:

NSURLRequest requestWithURL:<#(NSURL *)#>的CachePolicy:其中#(NSURLRequestCachePolicy)#> timeoutInterval:其中#(NSTimeInterval)#>

此政策可能更适合您的需求:NSURLRequestReloadIgnoringCacheData

或者您可以在URL的查询字符串中添加一些内容,以便在缓存中找不到它(例如,索引或时间戳)。