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 8789565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:26:37+00:00 2026-06-13T22:26:37+00:00

Possible Duplicate: php: pdf to string i am trying to save text content of

  • 0

Possible Duplicate:
php: pdf to string

i am trying to save text content of pdf file in to DB. I found this link helpful Converting PDF to string, and worked on it. But it only converts very less amount of data 🙁 why is it so ?

Or any other solution on how to convert complex pdf file (containing header, footer, tables, nd two column layout in some pages etc etc.) in to string and save it to DB ?

  • 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. Editorial Team
    Editorial Team
    2026-06-13T22:26:38+00:00Added an answer on June 13, 2026 at 10:26 pm

    A long time ago i wrote a script which download a pdf and convert it into text. This function do the convertion:

    function pdf2string($sourcefile) {
    
    $content = $sourcefile;
    
    $searchstart = 'stream';
    $searchend = 'endstream';
    $pdfText = '';
    $pos = 0;
    $pos2 = 0;
    $startpos = 0;
    
    while ($pos !== false && $pos2 !== false) {
    
    $pos = strpos($content, $searchstart, $startpos);
    $pos2 = strpos($content, $searchend, $startpos + 1);
    
    if ($pos !== false && $pos2 !== false){
    
    if ($content[$pos] == 0x0d && $content[$pos + 1] == 0x0a) {
    $pos += 2;
    } else if ($content[$pos] == 0x0a) {
    $pos++;
    }
    
    if ($content[$pos2 - 2] == 0x0d && $content[$pos2 - 1] == 0x0a) {
    $pos2 -= 2;
    } else if ($content[$pos2 - 1] == 0x0a) {
    $pos2--;
    }
    
    $textsection = substr(
    $content,
    $pos + strlen($searchstart) + 2,
    $pos2 - $pos - strlen($searchstart) - 1
    );
    $data = gzuncompress($textsection);
    $pdfText .= pdfExtractText($data);
    $startpos = $pos2 + strlen($searchend) - 1;
    
    }
    }
    
    return preg_replace('/(\s)+/', ' ', $pdfText);
    
    }
    

    EDIT: I call pdfExtractText() This function is defined here:

    function pdfExtractText($psData){
    
    if (!is_string($psData)) {
    return '';
    }
    
    $text = '';
    
    // Handle brackets in the text stream that could be mistaken for
    // the end of a text field. I'm sure you can do this as part of the
    // regular expression, but my skills aren't good enough yet.
    $psData = str_replace('\)', '##ENDBRACKET##', $psData);
    $psData = str_replace('\]', '##ENDSBRACKET##', $psData);
    
    preg_match_all(
    '/(T[wdcm*])[\s]*(\[([^\]]*)\]|\(([^\)]*)\))[\s]*Tj/si',
    $psData,
    $matches
    );
    for ($i = 0; $i < sizeof($matches[0]); $i++) {
    if ($matches[3][$i] != '') {
    // Run another match over the contents.
    preg_match_all('/\(([^)]*)\)/si', $matches[3][$i], $subMatches);
    foreach ($subMatches[1] as $subMatch) {
    $text .= $subMatch;
    }
    } else if ($matches[4][$i] != '') {
    $text .= ($matches[1][$i] == 'Tc' ? ' ' : '') . $matches[4][$i];
    }
    }
    
    // Translate special characters and put back brackets.
    $trans = array(
    '...' => '…',
    '\205' => '…',
    '\221' => chr(145),
    '\222' => chr(146),
    '\223' => chr(147),
    '\224' => chr(148),
    '\226' => '-',
    '\267' => '•',
    '\374'  => 'ü',
    '\344'  => 'ä',
    '\247'  => '§',
    '\366'  => 'ö',
    '\337'  => 'ß',
    '\334'  => 'Ü',
    '\326'  => 'Ö',
    '\304'  => 'Ä',
    '\(' => '(',
    '\[' => '[',
    '##ENDBRACKET##' => ')',
    '##ENDSBRACKET##' => ']',
    chr(133) => '-',
    chr(141) => chr(147),
    chr(142) => chr(148),
    chr(143) => chr(145),
    chr(144) => chr(146),
    );
    $text = strtr($text, $trans);
    
    return $text;
    }
    

    EDIT2: To get content from a local file use:

    $fp = fopen($sourcefile, 'rb');
    $content = fread($fp, filesize($sourcefile));
    fclose($fp);
    

    EDIT3: Before saving data to db i use an escape function:

    function escape($str)
    {
    $search=array("\\","\0","\n","\r","\x1a","'",'"');
    $replace=array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
    return str_replace($search,$replace,$str);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP - remove <img> tag from string I have my content like
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: PHP Get end string on url between / and / I have
Possible Duplicate: php loop through associative arrays i have this piece of code which
Possible Duplicate: PHP: Require() and Class Hierarchies Is this bad practice? Is there any
Possible Duplicate: open download dialog with php I have a link in my page
Possible Duplicate: PHP - send file to user I have written a script that
Possible Duplicate: PHP date time Trying to add one second to a datetime that
Possible Duplicate: PHP: self vs. $this What does $this-> mean in CakePHP? Please answer
Possible Duplicate: PHP: How to generate a random, unique, alphanumeric string? i want to

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.