gpt4 book ai didi

ios - 如何生成不分页的PDF

转载 作者:行者123 更新时间:2023-11-29 03:04:17 26 4
gpt4 key购买 nike

我正在使用以下代码将 WebView 转换为 PDF。

 UIWebView *webView = self.webView;
NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

int height = [heightStr intValue];
// CGRect screenRect = [[UIScreen mainScreen] bounds];
// CGFloat screenHeight = (self.contentWebView.hidden)?screenRect.size.width:screenRect.size.height;
CGFloat screenHeight = webView.bounds.size.height;
int pages = ceil(height / screenHeight);

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

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

[[[webView subviews] lastObject] setContentOffset:CGPointMake(0, screenHeight * 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 stringByAppendingString:@"/pdfFile.PDF"] ;

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
[webView setFrame:frame];

但问题是它生成的 PDF 带有分页符,如图所示。 enter image description here

我不想出现此分页符。有人可以建议我该怎么做吗?

最佳答案

你在这里设置页面大小

    UIGraphicsBeginPDFContextToData(pdfData, webView.bounds, nil);

当前设置为 webView.bounds

用适当的值改变它。

关于ios - 如何生成不分页的PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22954077/

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