我知道获取UIWebView的截图并将其转换为PDF但我需要生成一个正确的pdf(文本作为文本而不是截图).Save2PDF是一个创建适当PDF的应用程序.有人知道他们是怎么做到的吗?
I have done below code so far for UIView's viewPrintFormatter to be able create PDF:
Category
of UIPrintPageRenderer
for getting PDF Data@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end
@implementation UIPrintPageRenderer (PDF)
- (NSData*) printToPDF
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
[self prepareForDrawingPages: …
Run Code Online (Sandbox Code Playgroud) 我在这里有这个代码,我把UIView放入PDF中.我遇到的问题是我的视图是一个详细视图控制器并且是可滚动的,而PDF只获取当时详细视图控制器内部的内容而不是完整视图,如果我在详细视图中移动,它将捕获任何内容部分我在,而不是完整的事情.我正在尝试做什么?
- (IBAction)Share:(id)sender {
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, spreadSheet.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[spreadSheet.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"test.pdf"];
// …
Run Code Online (Sandbox Code Playgroud) 我想从 UIScrollView 的内容创建一个 PDF 文件。
func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String) {
let pdfData = NSMutableData()
let height = 1754.0
let width = 1240.0
UIGraphicsBeginPDFContextToData(pdfData, CGRect(x:-30, y:15,width:width,height:height) , nil)
UIGraphicsBeginPDFPage()
guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
aView.layer.render(in: pdfContext)
UIGraphicsEndPDFContext()
if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let documentsFileName = documentDirectories + "/" + fileName
debugPrint(documentsFileName)
pdfData.write(toFile: documentsFileName, atomically: true)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望代码生成一个面向滚动视图中内容大小的 PDF。
该代码目前仅生成静态 PDF。如果滚动视图中的内容大于 PDF 页面,则内容被截断。
关于如何将UIView呈现为PDF上下文的SO有几个问题,但它们都使用view.layer.renderInContext(pdfContext),这会产生72 DPI图像(以及打印时看起来很糟糕的图像).我正在寻找的是一种技术,以某种方式让UIView渲染类似300 DPI的东西.
这个问题之前已经发布过(从iphone应用程序中的UIScrollView创建PDF),我正在使用的代码来自这里.
这是代码
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories …
Run Code Online (Sandbox Code Playgroud)