ios - 如何将webview内容创建为pdf文件ios sdk
<p><div>
<aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
<div class="d-flex fd-column fw-nowrap">
<div class="d-flex fw-nowrap">
<div class="flex--item wmn0 fl1 lh-lg">
<div class="flex--item fl1 lh-lg">
<b>这个问题在这里已经有了答案</b>:
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使用 <code>UIWebView</code> 中的 <code>UIPrintPageRenderer</code> 执行以下步骤:</p>
<h3>添加<code>UIPrintPageRenderer</code>的<code>Category</code>获取PDF数据</h3>
<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>
<h3>为 A4 尺寸添加这些定义</h3>
<pre><code>#define kPaperSizeA4 CGSizeMake(595.2,841.8)
</code></pre>
<h3>现在在 <code>UIWebView's</code> <code>webViewDidFinishLoad</code> <code>delegate</code> 使用 <code>UIPrintPageRenderer</code> <code>property</code> 的 <code> UIWebView</code>.</h3>
<pre><code>- (void)webViewDidFinishLoad:(UIWebView *)awebView
{
if (awebView.isLoading)
return;
UIPrintPageRenderer *render = [ init];
;
//increase these values according to your requirement
float topPadding = 10.0f;
float bottomPadding = 10.0f;
float leftPadding = 10.0f;
float rightPadding = 10.0f;
CGRect printableRect = CGRectMake(leftPadding,
topPadding,
kPaperSizeA4.width-leftPadding-rightPadding,
kPaperSizeA4.height-topPadding-bottomPadding);
CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
forKey:@"paperRect"];
forKey:@"printableRect"];
NSData *pdfData = ;
if (pdfData) {
atomically: YES];
}
else
{
NSLog(@"PDF couldnot be created");
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何将webview内容创建为pdf文件ios sdk,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26272717/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26272717/
</a>
</p>
页:
[1]