2

我正在使用HTML文件在DocumentDirectory中生成 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)];
}


}
4

1 回答 1

1

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

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

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

NSURLRequest requestWithURL:<#(NSURL *)#> cachePolicy:<#(NSURLRequestCachePolicy)#> timeoutInterval:<#(NSTimeInterval)#>

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

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

于 2013-03-26T20:09:46.040 回答