Load PDF from documents directory - iPhone

后端 未结 3 436
小鲜肉
小鲜肉 2021-01-27 21:44

Instead of loading a PDF from the resources folder I would like to load it from the documents directory. I have been trying to do this for days but the CGPDFDocumentRef keeps

相关标签:
3条回答
  • 2021-01-27 22:15

    ok, next try. I hope this time it will be more useful :-/

    are you sure the file exists at this path?
    I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.

    I added this after your code sample to see if the pdf was loaded.

    NSLog(@"PDF: %@", pdf);
    int majorVersion;
    int minorVersion;
    CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
    NSLog(@"%d %d", majorVersion, minorVersion);
    

    and this gives me the following console output:

    2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
    2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
    2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
    2010-10-08 13:01:40.260 test[3517:207] 1 4
    

    there is clearly a %20 inside, so I think this is not the problem with your implementation.


    EDIT: Add this to your code to make sure that the file exists at this path.

    if (![[NSFileManager defaultManager] fileExistsAtPath:path])
        NSLog(@"File does not exist!");
    

    There must be a valid pdf-File at your path.

    0 讨论(0)
  • 2021-01-27 22:16

    The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.

    pdf = CGPDFDocumentCreateWithURL(pdfURL);

    0 讨论(0)
  • 2021-01-27 22:27

    You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.

    Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.

    0 讨论(0)
提交回复
热议问题