RE: Checking whether a file is a genuine PDF file
RE: Checking whether a file is a genuine PDF file
- Subject: RE: Checking whether a file is a genuine PDF file
- From: Squ Aire <email@hidden>
- Date: Mon, 5 Oct 2009 07:49:47 +0000
- Importance: Normal
<email@hidden>
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Thanks for the reply. However=2C of course a file can start with %PDF witho=
ut being a valid PDF file (and not openable by Preview).
I think I found an acceptable way of doing my thing. Just include the Quart=
z framework and do
PDFDocument *pdfDoc =3D [[PDFDocument alloc] initWithURL:fileURL]=3B
If pdfDoc is nil=2C we don't have a genuine PDF file. Otherwise=2C we do.
----------------------------------------
> Date: Sun=2C 4 Oct 2009 11:12:43 -0400
> Subject: Re: Checking whether a file is a genuine PDF file
> From: email@hidden
> To: email@hidden
> CC: email@hidden
>
> On Sun=2C Oct 4=2C 2009 at 8:07 AM=2C Squ Aire wrote:
>>
>> I know that=2C in an open dialog for example=2C I can view all PDF files=
by just filtering using the "pdf" file extension. But that isn't good enou=
gh really=2C because a file can have the pdf extension without being a true=
PDF file.
>
> From /usr/share/file/magic :
>
> # pdf: file(1) magic for Portable Document Format
> #
>
> 0 string %PDF- PDF document
>>5 byte x \b=2C version %c
>>7 byte x \b.%c
>
> So=2C a "real" PDF begins with "%PDF-"=2C then two bytes with the major
> and minor version numbers=2C respectively. See "man magic" for details.
>
>> How can I check whether a file is indeed a true PDF file (that can be=2C=
for example=2C opened in Preview)? Would I be able to filter true PDFs fil=
e in an open dialog=2C or would I just have to stick to filtering with only=
the pdf extension in open dialogs=2C and later ignore the non-valid PDF fi=
les?
>
> I would take the second approach. First=2C it's going to perform better=
=3B
> to filter the list in an open dialog=2C you'd have to open and verify
> every .pdf file in the directory being shown. You'd also need to
> inform the user somehow why some supposedly-pdf files are not
> selectable. Also=2C note that Preview's open dialog will allow you to
> select any file with a .pdf extension=2C then alerts the user if the
> file's contents aren't actually PDF data.
>
>> Do you have sample code that checks whether a given file is a genuine PD=
F file?
>
> Here's a simple command-line tool that checks a single file. Please
> keep in mind=2C this is just an example! In the Real World(tm)=2C you'd
> want more robust error checking of the number of arguments given=2C the
> size of the file before attempting to read the first 7 bytes=2C etc.
>
> #import=20
>
> int main (int argc=2C const char * argv[]) {
> NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]=3B
>
> NSString *filename =3D [[[NSProcessInfo processInfo] arguments] objectAtI=
ndex:1]=3B
> char buf[7]=3B
>
> [[NSData dataWithContentsOfMappedFile:filename] getBytes:(void*)buf lengt=
h:7]=3B
>
> if (strncmp("%PDF-"=2C buf=2C 5) =3D=3D 0)
> NSLog(@"%@ is a PDF file=2C version %c.%c"=2C filename=2C buf[5]=2C buf[6=
])=3B
> else
> NSLog(@"%@ is not a PDF file"=2C filename)=3B
>
> [pool drain]=3B
> return 0=3B
> }
>
> sherm--
>
> --
> Cocoa programming in Perl: http://camelbones.sourceforge.net
=0A=
_________________________________________________________________=0A=
Windows Live: Make it easier for your friends to see what you=92re up to on=
Facebook.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_2:092=
009=
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden