ios - 如何在 objective-c 中从uiwebview保存pdf
<p><p>我想在应用程序中从 UIWebView 将 pdf 保存到 ibook,UIWebView 中的 html 可以是多页的。<br/>
我尝试使用</p>
<pre><code>NSString *page = ;
NSString *path = ;
NSURL *targetURL = ;
self.docController = ;
if([ canOpenURL:])
{
;
NSLog(@"iBooks installed");
} else {
NSLog(@"iBooks not installed");
}
</code></pre>
<p>导出HTMLContentToPDF:</p>
<pre><code>CustomPrintPageRenderer *printPageRenderer = [ init];
UIMarkupTextPrintFormatter *printFormatter = [ initWithMarkupText:HTMLContent];
;//.addPrintFormatter(printFormatter, startingAtPageAtIndex: 0)
NSData *pdfData = ;
NSString *docDirPath = ;
NSString *fileName = ;
;//.writeToFile(fileName, atomically: true)
NSLog(@"%@", fileName);
return fileName;
</code></pre>
<p>问题是 <strong>exportHTMLContentToPDF</strong> 如果 UIWebView 包含多个页面,则只创建一个页面,并且有时输出格式不能很好地打印在 pdf 中</p>
<p>提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>从 UIWebview 中加载的任何 Microsoft 文档创建 PDF</strong></p>
<pre><code>#define kPaperSizeA4 CGSizeMake(595.2,841.8)
</code></pre>
<p>首先实现 UIPrintPageRenderer 协议(protocol)</p>
<pre><code>@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end
@implementation UIPrintPageRenderer (PDF)
- (NSData*) printToPDF
{
NSMutableData *pdfData = ;
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
;
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
;
}
UIGraphicsEndPDFContext();
return pdfData;
}
@end
</code></pre>
<p>然后,在 <code>UIWebView</code></p> 中文档加载完成后调用下面的方法
<pre><code>-(void)createPDF:(UIWebView *)webView {
UIPrintPageRenderer *render = [ init];
;
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));
forKey:@"paperRect"];
forKey:@"printableRect"];
NSData *pdfData = ;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (pdfData) {
;
}
else
{
NSLog(@"PDF couldnot be created");
}
});}
</code></pre>
<p>摘自 <a href="https://web.archive.org/web/20171006210548/https://stackoverflow.com/documentation/ios/2416/pdf-creation-in-ios/28437/create-pdf-from-any-microsoft-document-loaded-in-uiwebview#t=201710062105487361045" rel="noreferrer noopener nofollow">PDF Creation in iOS - Create PDF from any Microsoft Document loaded in UIWebview</a> .原作者是<a href="https://stackoverflow.com/users/1309698/mansi-panchal" rel="noreferrer noopener nofollow">Mansi Panchal</a> .归属细节可以在 <a href="https://web.archive.org/web/2/https://stackoverflow.com/documentation/contributors/example/28437" rel="noreferrer noopener nofollow">contributor page</a> 上找到。 .源在 <a href="https://creativecommons.org/licenses/by-sa/3.0/" rel="noreferrer noopener nofollow">CC BY-SA 3.0</a> 下获得许可并且可以在 <a href="https://archive.org/details/documentation-dump.7z" rel="noreferrer noopener nofollow">Documentation archive</a> 中找到.引用主题 ID:2416 和示例 ID:28437。</p></p>
<p style="font-size: 20px;">关于ios - 如何在 objective-c 中从uiwebview保存pdf,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43446153/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43446153/
</a>
</p>
页:
[1]