创建一个简单的 pdf

发布于 2024-12-05 21:02:58 字数 1802 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-12-12 21:02:58

您必须将新文件的名称添加到您的 URL 中。您的documentsURL现在包含以下内容:

/Users/jon/Library/Application Support/iPhone Simulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents/

You have to add the new file's name to your URL. Your documentsURL contains this right now:

/Users/jon/Library/Application Support/iPhone Simulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文