创建一个简单的 pdf
我正在尝试在 iPhone 上创建 PDF 来练习。我不知道为什么它不起作用。我收到错误:
2011-09-21 21:35:00.412 CreatePDF[869:b303] -[CreatePDFViewController createPDFDocumentAtURL:]
CreatePDF[869] <Error>: CGDataConsumerCreateWithFilename: failed to open `/Users/jon/Library/Application Support/iPhone Simulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents' for writing: Is a directory.
deflateEnd: error -3: (null).
CreatePDF[869] <Error>: CGPDFContextCreate: failed to create PDF context delegate.
2011-09-21 21:35:00.419 CreatePDF[869:b303] Couldn't create PDF context
<Error>: CGContextBeginPage: invalid context 0x0
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *documentsURL = [NSURL fileURLWithPath:documentsDirectory];
[self createPDFDocumentAtURL:(CFURLRef)documentsURL];
NSLog(@"%s", __FUNCTION__);
}
- (void)createPDFDocumentAtURL:(CFURLRef)url {
NSLog(@"%s", __FUNCTION__);
float red[] = {1., 0., 0., 1. };
CGRect mediaBox = CGRectMake(0, 0, 850, 1100);
CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &mediaBox, NULL);
if (!pdfContext) {
NSLog(@"Couldn't create PDF context");
}
CGContextBeginPage(pdfContext, &mediaBox);
CGContextSaveGState(pdfContext);
CGContextClipToRect(pdfContext, mediaBox);
CGContextSetFillColorSpace(pdfContext, CGColorSpaceCreateDeviceRGB());
CGContextSetFillColor(pdfContext, red);
CGContextFillRect(pdfContext, mediaBox);
CGContextRestoreGState(pdfContext);
CGContextEndPage(pdfContext);
CGContextRelease(pdfContext);
}
I'm trying to create a PDF on the iPhone for practice. I'm not sure why it doesn't work. I get the error:
2011-09-21 21:35:00.412 CreatePDF[869:b303] -[CreatePDFViewController createPDFDocumentAtURL:]
CreatePDF[869] <Error>: CGDataConsumerCreateWithFilename: failed to open `/Users/jon/Library/Application Support/iPhone Simulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents' for writing: Is a directory.
deflateEnd: error -3: (null).
CreatePDF[869] <Error>: CGPDFContextCreate: failed to create PDF context delegate.
2011-09-21 21:35:00.419 CreatePDF[869:b303] Couldn't create PDF context
<Error>: CGContextBeginPage: invalid context 0x0
Here is the code I have:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *documentsURL = [NSURL fileURLWithPath:documentsDirectory];
[self createPDFDocumentAtURL:(CFURLRef)documentsURL];
NSLog(@"%s", __FUNCTION__);
}
- (void)createPDFDocumentAtURL:(CFURLRef)url {
NSLog(@"%s", __FUNCTION__);
float red[] = {1., 0., 0., 1. };
CGRect mediaBox = CGRectMake(0, 0, 850, 1100);
CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &mediaBox, NULL);
if (!pdfContext) {
NSLog(@"Couldn't create PDF context");
}
CGContextBeginPage(pdfContext, &mediaBox);
CGContextSaveGState(pdfContext);
CGContextClipToRect(pdfContext, mediaBox);
CGContextSetFillColorSpace(pdfContext, CGColorSpaceCreateDeviceRGB());
CGContextSetFillColor(pdfContext, red);
CGContextFillRect(pdfContext, mediaBox);
CGContextRestoreGState(pdfContext);
CGContextEndPage(pdfContext);
CGContextRelease(pdfContext);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将新文件的名称添加到您的 URL 中。您的documentsURL现在包含以下内容:
You have to add the new file's name to your URL. Your documentsURL contains this right now: