Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 137147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:03:37+00:00 2026-05-11T07:03:37+00:00

I get a file via a HTTP upload and need to make sure its

  • 0

I get a file via a HTTP upload and need to make sure its a PDF file. The programing language is Python, but this should not matter.

I thought of the following solutions:

  1. Check if the first bytes of the string are %PDF. This is not a good check but prevents the user from uploading other files accidentally.

  2. Use libmagic (the file command in bash uses it). This does exactly the same check as in (1)

  3. Use a library to try to read the page count out of the file. If the lib is able to read a page count it should be a valid PDF file. Problem: I don’t know a Python library that can do this

Are there solutions using a library or another trick?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:03:38+00:00Added an answer on May 11, 2026 at 7:03 am

    The two most commonly used PDF libraries for Python are:

    • pypdf
    • ReportLab

    Both are pure python so should be easy to install as well be cross-platform.

    With pypdf it would probably be as simple as doing:

    from pypdf import PdfReader reader = PdfReader("upload.pdf") 

    This should be enough, but reader will now have the metadata and pages attributes if you want to do further checking.

    As Carl answered, pdftotext is also a good solution, and would probably be faster on very large documents (especially ones with many cross-references). However it might be a little slower on small PDF’s due to system overhead of forking a new process, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, result.content will contain the raw content of that page.… May 13, 2026 at 1:20 am
  • Editorial Team
    Editorial Team added an answer Seems that the files are not really part of the… May 13, 2026 at 1:20 am
  • Editorial Team
    Editorial Team added an answer Change your binding to TemplateBinding. eg <dg:DataGrid.RowStyle> <Style TargetType="dg:DataGridRow"> <Setter… May 13, 2026 at 1:20 am

Related Questions

In my code attached below, I'm trying to upload a file via ASP.NET. I
Since all files in a web project are compiled into single assembly, then does
Ok, I've tried about near everything and I cannot get this to work. I
We use the ExpressionEngine CMS (php) to create websites. For each site, we set

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.