gpt4 book ai didi

ios - 从 UIWebView 创建 PDF 文件

转载 作者:IT王子 更新时间:2023-10-29 08:10:28 24 4
gpt4 key购买 nike

 -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
UIWebView *webView = (UIWebView*)aView;
NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

int height = [heightStr intValue];

// Get the number of pages needed to print. 9 * 72 = 648
int pages = ceil(height / 648.0);

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );
CGRect frame = [webView frame];
for (int i = 0; i < pages; i++) {
// Check to see if page draws more than the height of the UIWebView
if ((i+1) * 648 > height) {
CGRect f = [webView frame];
f.size.height -= (((i+1) * 648.0) - height);
[webView setFrame: f];
}

UIGraphicsBeginPDFPage();
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 72, 72); // Translate for 1" margins

[[[webView subviews] lastObject] setContentOffset:CGPointMake(0, 648 * i) animated:NO];
[webView.layer renderInContext:currentContext];
}

UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
[webView setFrame:frame];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

我正在使用上面的代码从 WebView 生成 pdf 文件。它正在工作,但是内容没有正确裁剪:页面底部的内容被弄乱了。我想我可以使用 Core Graphics 方法做一些更好的事情,但我找不到如何去做。有什么想法吗?

最佳答案

使用 UIWebView 中的 UIPrintPageRenderer 按照以下步骤操作:

添加 UIPrintPageRendererCategory 获取 PDF 数据

@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end

@implementation UIPrintPageRenderer (PDF)
- (NSData*) printToPDF
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
[self drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
return pdfData;
}
@end

为 A4 尺寸添加这些定义

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

现在在 UIWebView 的 webViewDidFinishLoad delegate 中使用 UIPrintPageRenderer property UIWebView.

- (void)webViewDidFinishLoad:(UIWebView *)awebView
{
if (awebView.isLoading)
return;

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];
//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);
[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
NSData *pdfData = [render printToPDF];
if (pdfData) {
[pdfData writeToFile:[NSString stringWithFormat:@"%@/tmp.pdf",NSTemporaryDirectory()] atomically: YES];
}
else
{
NSLog(@"PDF couldnot be created");
}
}

关于ios - 从 UIWebView 创建 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15813005/

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