文字

PDF 函数

Remarks about Deprecated PDFlib Functions

Starting with PHP 4.0.5, the PHP extension for PDFlib is officially supported by PDFlib GmbH. This means that all the functions described in the PDFlib Reference Manual are supported by PHP 4 with exactly the same meaning and the same parameters. However, with PDFlib Version 5.0.4 or higher all parameters have to be specified. For compatibility reasons, this binding for PDFlib still supports most of the deprecated functions, but they should be replaced by their new versions. PDFlib GmbH will not support any problems arising from the use of these deprecated functions. The documentation in this section indicates old functions as "Deprecated" and gives the replacement function to be used instead.

Table of Contents

  • PDF_activate_item — Activate structure element or other content item
  • PDF_add_annotation — Add annotation [deprecated]
  • PDF_add_bookmark — Add bookmark for current page [deprecated]
  • PDF_add_launchlink — Add launch annotation for current page [deprecated]
  • PDF_add_locallink — Add link annotation for current page [deprecated]
  • PDF_add_nameddest — Create named destination
  • PDF_add_note — Set annotation for current page [deprecated]
  • PDF_add_outline — Add bookmark for current page [deprecated]
  • PDF_add_pdflink — Add file link annotation for current page [deprecated]
  • PDF_add_table_cell — Add a cell to a new or existing table
  • PDF_add_textflow — Create Textflow or add text to existing Textflow
  • PDF_add_thumbnail — Add thumbnail for current page
  • PDF_add_weblink — Add weblink for current page [deprecated]
  • PDF_arc — Draw a counterclockwise circular arc segment
  • PDF_arcn — Draw a clockwise circular arc segment
  • PDF_attach_file — Add file attachment for current page [deprecated]
  • PDF_begin_document — Create new PDF file
  • PDF_begin_font — Start a Type 3 font definition
  • PDF_begin_glyph — Start glyph definition for Type 3 font
  • PDF_begin_item — Open structure element or other content item
  • PDF_begin_layer — Start layer
  • PDF_begin_page_ext — Start new page
  • PDF_begin_page — Start new page [deprecated]
  • PDF_begin_pattern — Start pattern definition
  • PDF_begin_template_ext — Start template definition
  • PDF_begin_template — Start template definition [deprecated]
  • PDF_circle — Draw a circle
  • PDF_clip — Clip to current path
  • PDF_close_image — Close image
  • PDF_close_pdi_page — Close the page handle
  • PDF_close_pdi — Close the input PDF document [deprecated]
  • PDF_close — Close pdf resource [deprecated]
  • PDF_closepath_fill_stroke — Close, fill and stroke current path
  • PDF_closepath_stroke — Close and stroke path
  • PDF_closepath — Close current path
  • PDF_concat — Concatenate a matrix to the CTM
  • PDF_continue_text — Output text in next line
  • PDF_create_3dview — Create 3D view
  • PDF_create_action — Create action for objects or events
  • PDF_create_annotation — Create rectangular annotation
  • PDF_create_bookmark — Create bookmark
  • PDF_create_field — Create form field
  • PDF_create_fieldgroup — Create form field group
  • PDF_create_gstate — Create graphics state object
  • PDF_create_pvf — Create PDFlib virtual file
  • PDF_create_textflow — Create textflow object
  • PDF_curveto — Draw Bezier curve
  • PDF_define_layer — Create layer definition
  • PDF_delete_pvf — Delete PDFlib virtual file
  • PDF_delete_table — Delete table object
  • PDF_delete_textflow — Delete textflow object
  • PDF_delete — Delete PDFlib object
  • PDF_encoding_set_char — Add glyph name and/or Unicode value
  • PDF_end_document — Close PDF file
  • PDF_end_font — Terminate Type 3 font definition
  • PDF_end_glyph — Terminate glyph definition for Type 3 font
  • PDF_end_item — Close structure element or other content item
  • PDF_end_layer — Deactivate all active layers
  • PDF_end_page_ext — Finish page
  • PDF_end_page — Finish page
  • PDF_end_pattern — Finish pattern
  • PDF_end_template — Finish template
  • PDF_endpath — End current path
  • PDF_fill_imageblock — Fill image block with variable data
  • PDF_fill_pdfblock — Fill PDF block with variable data
  • PDF_fill_stroke — Fill and stroke path
  • PDF_fill_textblock — Fill text block with variable data
  • PDF_fill — Fill current path
  • PDF_findfont — Prepare font for later use [deprecated]
  • PDF_fit_image — Place image or template
  • PDF_fit_pdi_page — Place imported PDF page
  • PDF_fit_table — Place table on page
  • PDF_fit_textflow — Format textflow in rectangular area
  • PDF_fit_textline — Place single line of text
  • PDF_get_apiname — Get name of unsuccessfull API function
  • PDF_get_buffer — Get PDF output buffer
  • PDF_get_errmsg — Get error text
  • PDF_get_errnum — Get error number
  • PDF_get_font — Get font [deprecated]
  • PDF_get_fontname — Get font name [deprecated]
  • PDF_get_fontsize — Font handling [deprecated]
  • PDF_get_image_height — Get image height [deprecated]
  • PDF_get_image_width — Get image width [deprecated]
  • PDF_get_majorversion — Get major version number [deprecated]
  • PDF_get_minorversion — Get minor version number [deprecated]
  • PDF_get_parameter — Get string parameter
  • PDF_get_pdi_parameter — Get PDI string parameter [deprecated]
  • PDF_get_pdi_value — Get PDI numerical parameter [deprecated]
  • PDF_get_value — Get numerical parameter
  • PDF_info_font — Query detailed information about a loaded font
  • PDF_info_matchbox — Query matchbox information
  • PDF_info_table — Retrieve table information
  • PDF_info_textflow — Query textflow state
  • PDF_info_textline — Perform textline formatting and query metrics
  • PDF_initgraphics — Reset graphic state
  • PDF_lineto — Draw a line
  • PDF_load_3ddata — Load 3D model
  • PDF_load_font — Search and prepare font
  • PDF_load_iccprofile — Search and prepare ICC profile
  • PDF_load_image — Open image file
  • PDF_makespotcolor — Make spot color
  • PDF_moveto — Set current point
  • PDF_new — Create PDFlib object
  • PDF_open_ccitt — Open raw CCITT image [deprecated]
  • PDF_open_file — Create PDF file [deprecated]
  • PDF_open_gif — Open GIF image [deprecated]
  • PDF_open_image_file — Read image from file [deprecated]
  • PDF_open_image — Use image data [deprecated]
  • PDF_open_jpeg — Open JPEG image [deprecated]
  • PDF_open_memory_image — Open image created with PHP's image functions [not supported]
  • PDF_open_pdi_document — Prepare a pdi document
  • PDF_open_pdi_page — Prepare a page
  • PDF_open_pdi — Open PDF file [deprecated]
  • PDF_open_tiff — Open TIFF image [deprecated]
  • PDF_pcos_get_number — Get value of pCOS path with type number or boolean
  • PDF_pcos_get_stream — Get contents of pCOS path with type stream, fstream, or string
  • PDF_pcos_get_string — Get value of pCOS path with type name, string, or boolean
  • PDF_place_image — Place image on the page [deprecated]
  • PDF_place_pdi_page — Place PDF page [deprecated]
  • PDF_process_pdi — Process imported PDF document
  • PDF_rect — Draw rectangle
  • PDF_restore — Restore graphics state
  • PDF_resume_page — Resume page
  • PDF_rotate — Rotate coordinate system
  • PDF_save — Save graphics state
  • PDF_scale — Scale coordinate system
  • PDF_set_border_color — Set border color of annotations [deprecated]
  • PDF_set_border_dash — Set border dash style of annotations [deprecated]
  • PDF_set_border_style — Set border style of annotations [deprecated]
  • PDF_set_char_spacing — Set character spacing [deprecated]
  • PDF_set_duration — Set duration between pages [deprecated]
  • PDF_set_gstate — Activate graphics state object
  • PDF_set_horiz_scaling — Set horizontal text scaling [deprecated]
  • PDF_set_info_author — Fill the author document info field [deprecated]
  • PDF_set_info_creator — Fill the creator document info field [deprecated]
  • PDF_set_info_keywords — Fill the keywords document info field [deprecated]
  • PDF_set_info_subject — Fill the subject document info field [deprecated]
  • PDF_set_info_title — Fill the title document info field [deprecated]
  • PDF_set_info — Fill document info field
  • PDF_set_layer_dependency — Define relationships among layers
  • PDF_set_leading — Set distance between text lines [deprecated]
  • PDF_set_parameter — Set string parameter
  • PDF_set_text_matrix — Set text matrix [deprecated]
  • PDF_set_text_pos — Set text position
  • PDF_set_text_rendering — Determine text rendering [deprecated]
  • PDF_set_text_rise — Set text rise [deprecated]
  • PDF_set_value — Set numerical parameter
  • PDF_set_word_spacing — Set spacing between words [deprecated]
  • PDF_setcolor — Set fill and stroke color
  • PDF_setdash — Set simple dash pattern
  • PDF_setdashpattern — Set dash pattern
  • PDF_setflat — Set flatness
  • PDF_setfont — Set font
  • PDF_setgray_fill — Set fill color to gray [deprecated]
  • PDF_setgray_stroke — Set stroke color to gray [deprecated]
  • PDF_setgray — Set color to gray [deprecated]
  • PDF_setlinecap — Set linecap parameter
  • PDF_setlinejoin — Set linejoin parameter
  • PDF_setlinewidth — Set line width
  • PDF_setmatrix — Set current transformation matrix
  • PDF_setmiterlimit — Set miter limit
  • PDF_setpolydash — Set complicated dash pattern [deprecated]
  • PDF_setrgbcolor_fill — Set fill rgb color values [deprecated]
  • PDF_setrgbcolor_stroke — Set stroke rgb color values [deprecated]
  • PDF_setrgbcolor — Set fill and stroke rgb color values [deprecated]
  • PDF_shading_pattern — Define shading pattern
  • PDF_shading — Define blend
  • PDF_shfill — Fill area with shading
  • PDF_show_boxed — Output text in a box [deprecated]
  • PDF_show_xy — Output text at given position
  • PDF_show — Output text at current position
  • PDF_skew — Skew the coordinate system
  • PDF_stringwidth — Return width of text
  • PDF_stroke — Stroke path
  • PDF_suspend_page — Suspend page
  • PDF_translate — Set origin of coordinate system
  • PDF_utf16_to_utf8 — Convert string from UTF-16 to UTF-8
  • PDF_utf32_to_utf16 — Convert string from UTF-32 to UTF-16
  • PDF_utf8_to_utf16 — Convert string from UTF-8 to UTF-16

用户评论:

[#1] sebastien at malot dot fr [2013-10-26 10:47:46]

Hi,

I post this comment here because I always wanted to extract text from PDF files, but I never found it.
So now, I spread my tresor.

So spend much time in creating a PHP library to extract text from pages.
Based on TCPDF parser class, now my lib can handle many cases such as multiple charset encoding, base64 and octal encoding ...

Project webiste : http://www.pdfparser.org

<?php

// Include Composer autoloader if not already done.
include 'vendor/autoload.php';
 
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf    $parser->parseFile('document.pdf');
 
// Retrieve all pages from the pdf file.
$pages  $pdf->getPages();
 
// Loop over each page to extract text.
foreach ($pages as $page) {
    echo 
$page->getText();
}

?>


Don't hesitate to report any bug on github issue report tool.

Thanks

[#2] sebastien at malot dot fr [2013-08-08 10:02:10]

Do you have a sample pdf ?
Can you try those classes :

http://gist.github.com/smalot/6183152
or
http://www.zikko.se/resources/extractFromPDF.php

can you see your links on the extracted content ?
BR

[#3] bolyde at gmail dot com [2009-04-12 12:27:44]

Hi,
To find the page number of a PDF File, i find this :

<?php
public function getNumPagesInPDF(array $arguments = array()) 
{
@list(
$PDFPath) = $arguments;
$stream = @fopen($PDFPath"r");
$PDFContent = @fread ($streamfilesize($PDFPath));
if(!
$stream || !$PDFContent)
    return 
false;
    
$firstValue 0;
$secondValue 0;
if(
preg_match("/\/N\s+([0-9]+)/"$PDFContent$matches)) {
    
$firstValue $matches[1];
}
 
if(
preg_match_all("/\/Count\s+([0-9]+)/s"$PDFContent$matches))
{
    
$secondValue max($matches[1]);
}
return ((
$secondValue != 0) ? $secondValue max($firstValue$secondValue));
}
?>

[#4] bondo2 at bondo2 dot info [2008-10-09 02:20:05]

<?php

//getting new instance 
$pdfFile new_pdf();

PDF_open_file($pdfFile" ");

//document info
pdf_set_info($pdfFile"Auther""Ahmed Elbshry");
pdf_set_info($pdfFile"Creator""Ahmed Elbshry");
pdf_set_info($pdfFile"Title""PDFlib");
pdf_set_info($pdfFile"Subject""Using PDFlib");

//starting our page and define the width and highet of the document
pdf_begin_page($pdfFile595842);

//check if Arial font is found, or exit
if($font PDF_findfont($pdfFile"Arial""winansi"1)) {
    
PDF_setfont($pdfFile$font12);
} else {
    echo (
"Font Not Found!");
    
PDF_end_page($pdfFile);
    
PDF_close($pdfFile);
    
PDF_delete($pdfFile);
    exit();
}

//start writing from the point 50,780
PDF_show_xy($pdfFile"This Text In Arial Font"50780);
PDF_end_page($pdfFile);
PDF_close($pdfFile);

//store the pdf document in $pdf
$pdf PDF_get_buffer($pdfFile);
//get  the len to tell the browser about it
$pdflen strlen($pdfFile);

//telling the browser about the pdf document
header("Content-type: application/pdf");
header("Content-length: $pdflen");
header("Content-Disposition: inline; filename=phpMade.pdf");
//output the document
print($pdf);
//delete the object
PDF_delete($pdfFile);
?>

[#5] SID TRIVEDI [2008-01-20 06:16:10]

[#6] info at tecnick dot com [2008-01-10 00:54:49]

For those of us that do not want to pay for a commercial license to use PDFlib I suggest TCPDF:

http://tcpdf.sf.net

TCPDF is an Open Source PHP class for generating PDF files on-the-fly without requiring external extensions. This class is already adopted by a large number of php projects such as phpMyAdmin, Drupal, Joomla, Xoops, TCExam, etc.  

Starting from 2.1 version TCPDF supports UTF-8 Unicode and bidirectional languages such as Arabic and Hebrew.

[#7] Ken McColl [2007-11-21 05:06:03]

To get this to work on Windows do not use escapeshellcmd()

From online help:
Following characters are preceded by a backslash: #&;`|*?~<>^()[]{}$\, \x0A and \xFF. ' and " are escaped only if they are not paired. In Windows, all these characters plus % are replaced by a space instead.

So you are probably passing duff paths to pdf2text.exe

Removing escapeshellcmd worked for me. Just make darned sure you are in control of what is being passed through to your system call.

[#8] kangaroo232002 at yahoo dot co dot uk [2007-11-18 12:25:47]

To extend alex's example earlier, you can use a couple of switches inside the pdf doc to give you the total number of pages, without using any ext. I would have added the whole code, however the site keeps on saying "line is too long... yadayada".

Open the doc using fopen("$file", "rb"); (for reading)

Test the first approx 1000b for the following regex
<?php
if(preg_match("/\/N\s+([0-9]+)/"$contents$found)) {
    return 
$found[1];
}
?>


If that doesn't return anything, you have to read the rest of the file:

<?php

preg_match_all
("/\/Type\s*\/Pages\s*\/Kids\s+
\[.*?\]\s*\/Count\s+([0-9]+)/"
);

?>


This may return more than one, so look through for the highest value, which is the total number of pages in your doc.

[#9] luc at phpt dot org [2007-03-29 21:09:17]

I am trying to extract the text from PDF files and use it to feed a search engine (Intranet tool). I tried several functions "PDF2TXT" posted below, but not they do not produce the expected result. At least, all words need to be separated by spaces (then used as keywords), and the "junk" codes removed (for example: binary data, pictures...). I start modifying the interesting function posted by Swen, and here is the my current version that starts to work quite well (with PDF version 1.2). Sorry for having a quite different style of programming. Luc

<?php
// Patch for pdf2txt() posted Sven Schuberth
// Add/replace following code (cannot post full program, size limitation)

// handles the verson 1.2
// New version of handleV2($data), only one line changed
function handleV2($data){
        
    
// grab objects and then grab their contents (chunks)
    
$a_obj getDataArray($data,"obj","endobj");
    
    foreach(
$a_obj as $obj){
        
        
$a_filter getDataArray($obj,"<<",">>");
    
        if (
is_array($a_filter)){
            
$j++;
            
$a_chunks[$j]["filter"] = $a_filter[0];

            
$a_data getDataArray($obj,"stream\r\n","endstream");
            if (
is_array($a_data)){
                
$a_chunks[$j]["data"] = substr($a_data[0],
        
strlen("stream\r\n"),
        
strlen($a_data[0])-strlen("stream\r\n")-strlen("endstream"));
            }
        }
    }

    
// decode the chunks
    
foreach($a_chunks as $chunk){

        
// look at each chunk and decide how to decode it - by looking at the contents of the filter
        
$a_filter split("/",$chunk["filter"]);
        
        if (
$chunk["data"]!=""){
            
// look at the filter to find out which encoding has been used            
            
if (substr($chunk["filter"],"FlateDecode")!==false){
                
$data =@ gzuncompress($chunk["data"]);
                if (
trim($data)!=""){
            
// CHANGED HERE, before: $result_data .= ps2txt($data);    
                    
$result_data .= PS2Text_New($data);
                } else {
                
                    
//$result_data .= "x";
                
}
            }
        }
    }
    return 
$result_data;
}

// New function - Extract text from PS codes
function ExtractPSTextElement($SourceString)
{
$CurStartPos 0;
while ((
$CurStartText strpos($SourceString'('$CurStartPos)) !== FALSE)
    {
    
// New text element found
    
if ($CurStartText $CurStartPos 8$Spacing ' ';
    else    {
        
$SpacingSize substr($SourceString$CurStartPos$CurStartText $CurStartPos);
        if (
$SpacingSize < -25$Spacing ' '; else $Spacing '';
        }
    
$CurStartText++;

    
$StartSearchEnd $CurStartText;
    while ((
$CurStartPos strpos($SourceString')'$StartSearchEnd)) !== FALSE)
        {
        if (
substr($SourceString$CurStartPos 11) != '\\') break;
        
$StartSearchEnd $CurStartPos 1;
        }
    if (
$CurStartPos === FALSE) break; // something wrong happened
    
    // Remove ending '-'
    
if (substr($Result, -11) == '-')
        {
        
$Spacing '';
        
$Result substr($Result0, -1);
        }

    
// Add to result
    
$Result .= $Spacing substr($SourceString$CurStartText$CurStartPos $CurStartText);
    
$CurStartPos++;
    }
// Add line breaks (otherwise, result is one big line...)
return $Result "\n";
}

// Global table for codes replacement 
$TCodeReplace = array ('\(' => '(''\)' => ')');

// New function, replacing old "pd2txt" function
function PS2Text_New($PS_Data)
{
global 
$TCodeReplace;

// Catch up some codes
if (ord($PS_Data[0]) < 10) return ''
if (
substr($PS_Data08) == '/CIDInit') return '';

// Some text inside (...) can be found outside the [...] sets, then ignored 
// => disable the processing of [...] is the easiest solution

$Result ExtractPSTextElement($PS_Data);

// echo "Code=$PS_Data\nRES=$Result\n\n";

// Remove/translate some codes
return strtr($Result$TCodeReplace);
}

?>

[#10] brendandonhue at comcast dot net [2006-08-22 08:35:05]

Here is a function to test whether a file is a PDF without using any external library.
<?php
define
('PDF_MAGIC'"\\x25\\x50\\x44\\x46\\x2D");
function 
is_pdf($filename) {
  return (
file_get_contents($filenamefalsenull0strlen(PDF_MAGIC)) === PDF_MAGIC) ? true false;
}
?>

It's not checking if the whole file is valid, just if the correct header is present at the beginning of the file.

[#11] MAGnUm at magnumhome dot servehttp.com [2006-07-17 14:01:32]

domPDF is also a great PDF creation interface. it basically converts your code to CSS and then builds the PDF from that with the absolute positions, and what not...

[#12] spingary at yahoo dot com [2006-01-12 12:55:01]

I was having trouble with streaming inline PDf's using PHP 5.0.2, Apache 2.0.54.

This is my code:

<?php
header
("Pragma: public");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate");
header("Content-type: application/pdf");
header("Content-Length: ".filesize($file));
header("Content-disposition: inline; filename=$file");
header("Accept-Ranges: ".filesize($file)); 
readfile($file);
exit();
?>

It would work fine in Mozilla Firefox (1.0.7) but with IE (6.0.2800.1106) it would not bring up the Adobe Reader plugin and instead ask me to save it or open it as a PHP file.

Oddly enough, I turned off ZLib.compression and it started working.  I guess the compression is confusing IE.  I tried leaving out the content-length header thinking maybe it was unmatched filesize (uncompressed number vs actual received compressed size), but then without it it screws up Firefox too.  

What I ended up doing was disabling Zlib compression for the PDF output pages using ini_set:

<?php
ini_set
('zlib.output_compression','Off'); 
?>


Maybe this will help someone. Will post over in the PDF section as well.

[#13] ontwerp AT zonnet.nl [2005-11-03 11:01:18]

I was searching for a lowcost/opensource option for combining static html files [as templates] and dynamic output from perl or php routines etc. And the sooner or later I found out that this was the most stable, 'speedest' and customizeable way to produce usable pdf 's with nice formatting :

1] create html page output [perl-> html output, direct html output from any app or php echo's etc. [sort these html files locally]

2] parse all html [inluding webimages links, tables font formatting etc] to [E]PS files with the perl app : html2ps [as mentioned beneath] 
http://user.it.uu.se/~jan/html2ps.html [sort all ps files by future pdf page positions]

3] use the free ps2pdf/ps2pdfwr linux application 
http://www.ps2pdf.com/convert/index.htm [uses gostscript, ghostview libs and so on etc]
Has great formatting options like headers, footers, numbering etc
[sort pdf files]

4] convert all pdf files to 1 pdf file with : pdftk [pdftoolkit], deliveres optional compressions/encryption, background stamps etc

One should ask why using different scripts :
- combination perl/php is great : perl is speedier at some issues like conversion to ps files in my experience
- ps to pdf is quickier then direct php to pdf [in my exp.!]
- I have total control over every files whenever i change html files as a template I use only editors or other app. for it [online or offline].

p.s. I had to make a opensource solution for creating simpel report analyses that's based on things like :
- first page [name / title / #/ date]
- some static info [like introduction, copyrights etc]
- some dynamic info [outputted from php->dbase queries] combined
with html tags/images etc.

And this all mixed [so seperated in files for transparancy]. Also the 3 way manner : data-> html, html->ps, ps->pdf, is easier and quickier to program or adjust in every step.

Correct me if i'm wrong [mail me to]

ing. Valentijn Langendorff
Design & Technologist

[#14] ragnar at deulos dot com [2005-10-07 19:30:58]

After one hole day understanding how pdflib works i got the conclusion that its enough hard to draw just with words to furthermore for drawing a line maybe you will need something like four lines of code, so i did my own functions to do the life easier and the code more understable to modify and draw. I also made a function that will draw a rect with the corners round and the posibility even to fill it ;)

You can get it from http://www.deulos.com/pdf_php.php

feel free to make suggestions or whatever u like ;o)

[#15] thodge at ipswich dot qld dot gov dot au [2005-09-04 22:22:40]

Yet another addition to the PDF text extraction code last posted by jorromer. The code only seemed to work for PDF 1.2 (Acrobat 3.x) or below. This pdfExtractText function uses regular expressions to cover cases I have found in PDF 1.3 and 1.4 documents. The code also handles closing brackets in the text stream, which were ignored by the previous version. My regular expression skills are somewhat lacking, so improvements may possible by a more skilled programmer. I'm sure there are still cases that this function will not handle, but I haven't come across any yet...

<?php

function pdf2string($sourcefile) {

    
$fp fopen($sourcefile'rb');
    
$content fread($fpfilesize($sourcefile));
    
fclose($fp);

    
$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);

}

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'                => '?',
        
'\('                => '(',
        
'\['                => '[',
        
'##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;

}

?>

[#16] jorromer at uchile dot cl -- Krash [2005-06-07 10:51:23]

I recently use mattb code below for the extraction of text from PDF files. I modify this code for only extract text fields.

Hope i can help some one

Here is the Function

<?php

  $text 
pdf2string("file.pdf");
  echo 
$text;

  function 
pdf2string($sourcefile){
    
$fp fopen($sourcefile'rb');
    
$content fread($fpfilesize($sourcefile));
    
fclose($fp);

    
$searchstart 'stream';
    
$searchend 'endstream';
    
$pdfdocument '';
    
$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);
        
$data ExtractText2($data);
        
$startpos $pos2 strlen($searchend) - 1;
        
        if (
$data === false){ 
          return -
1;}
          
        
$pdfdocument .= $data;}}
   return 
$pdfdocument;}

function 
ExtractText2($postScriptData){
  
$sw true;
  
$textStart 0;
  
$len strlen($postScriptData);

  while (
$sw){
    
$ini strpos($postScriptData'('$textStart);
    
$end strpos($postScriptData')'$textStart+1);
    if ((
$ini>0) && ($end>$ini)){
      
$valtext strpos($postScriptData,'Tj',$end+1);
      if (
$valtext == $end 2)
        
$text .= substr($postScriptData,$ini+1,$end $ini 1);}
      
    
$textStart $end 1;
    if (
$len<=$textStart$sw=false;
    
    if ((
$ini == 0) && ($end == 0)) $sw=false;}
  
  
$trans = array("\\341" => "a","\\351" => "e","\\355" => "i","\\363" => "o","\\223" => "","\\224" => "");
  
$text  strtr($text$trans);
  return 
$text;

?>

[#17] webadmin at secretscreen dot com [2005-04-05 14:51:25]

I found this info about pdflib scope on a Chinese (I think) site and translated it.  I was trying to do pdf_setfont and kept getting the wrong scope error.  Turns out it has to be in the Page scope.  So pdf_setfont will only work when called between pdf_begin_page and pdf_end_page.

#########################################
When API of the PDFlib is called, the error, Can't - IN 'document' scope occurs 
There is a concept of " the scope " in the PDFlib, as for all API of the PDFlib it is called with some scope, the *1 which is decided This error occurs when it is called other than the scope where API is appointed. The chart below in reference, please verify API call position.

Path: PDF_moveto (), PDF_circle (), PDF_arc (), PDF_arcn (), PDF_rect () in each case PDF_stroke (), PDF_closepath_stroke (), PDF_fill (), PDF_fill_stroke (), PDF_closepath_fill_stroke (), PDF_clip (), PDF_endpath () the between 

Page: PDF_begin_page () with PDF_end_page () in between outside path  

Template: PDF_begin_template () with PDF_end_template () in between outside path  

Pattern: PDF_begin_pattern () with PDF_end_pattern () in between outside path  

Font: PDF_begin_font () with PDF_end_font () in between outside glyph  

Glyph: PDF_begin_glyph () with PDF_end_glyph () in between outside path  

Document: PDF_open_* () with PDF_close () in between outside page tempalte and pattern  

Object: The PDF_new () with the PDF_delete () it belongs to the other no scope in between the place 

Null: Outside object  

Any: All scopes other than  

##########################################

Hope this helps others as much as it helped me!!!

[#18] chu61 dot tw at gmail dot com [2005-03-06 19:57:15]

How to get how many pages in a PDF? I read PDF spec. V1.6 and find this:

PDF set  a "Page Tree Node" to define the ordering of pages in the document. The tree structure allows PDF applications, using little memory to quickly open a document containing thousands of pages.

If a PDF have 63 pages, the page tree node will like this...

2 0 obj
<< /Type /Pages
    /Kidsn [ 4 0 R
               10 0 R
             ]
     /Count 63        <---- YES, got it
>>
endobj

[P.S]   a  PDF may not only a pages tree node, The right answer is in "root page tree node", if  /Count XX with  /Parent XXX node, it not "root page tree node"

SO, You must find the node with /Count XX and Without /Parent  terms, and you'll get total pages of PDF

%PDF-1.0  ~  %PDF-1.5 all works

Alex form Taipei,Taiwan

[#19] michi (Alt+Q) marel.at [2004-07-01 07:10:43]

<?PHP

function calcToPt($intMillimeter) {
  
$intPoints = ($intMillimeter*72)/25.4;
  
$intPoints round($intPoints);
  return 
$intPoints;
}


pdf_begin_page$pdfcalcToPt(210), calcToPt(297)); // 595x842 pt
?>

[#20] donatas at spurgius dot com [2004-06-22 12:56:55]

I've been looking for a way to extract plain text from PDF documents (needed to search for text inside 'em). Not being able to find one I wrote the needed functions myself. here you go folks.

<?php
  
function pdf2string ($sourceFile)
  {
    
$textArray = array ();
    
$objStart 0;
    
    
$fp fopen ($sourceFile'rb');
    
$content fread ($fpfilesize ($sourceFile));
    
fclose ($fp);
    
    
$searchTagStart chr(13).chr(10).'stream';
    
$searchTagStartLenght strlen ($searchTagStart);
    
    while (((
$objStart strpos ($content$searchTagStart$objStart)) && ($objEnd strpos ($content'endstream'$objStart+1))))
    {
      
$data substr ($content$objStart $searchTagStartLenght 2$objEnd - ($objStart $searchTagStartLenght) - 2);
      
$data = @gzuncompress ($data);
      
      if (
$data !== FALSE && strpos ($data'BT') !== FALSE && strpos ($data'ET') !== FALSE)
      {
        
$textArray [] = ExtractText ($data);
      }
      
      
$objStart $objStart $objEnd $objEnd $objStart 1;
    }
    
    return 
$textArray;
  }
  
  function 
ExtractText ($postScriptData)
  {
    while (((
$textStart strpos ($postScriptData'('$textStart)) && ($textEnd strpos ($postScriptData')'$textStart 1)) && substr ($postScriptData$textEnd 1) != '\\'))
    {
      
$plainText .= substr ($postScriptData$textStart 1$textEnd $textStart 1);
      if (
substr ($postScriptData$textEnd 11) == ']'//this adds quite some additional spaces between the words
      
{
        
$plainText .= ' ';
      }
      
      
$textStart $textStart $textEnd $textEnd $textStart 1;
    }
    
    return 
stripslashes ($plainText);
  }
?>

[#21] uwe at steinmann dot cx [2004-05-13 06:25:13]

Those looking for a free replacement of pdflib may consider
pslib at http://pslib.sourceforge.net which produces PostScript but it can be easily turned into PDF by Acrobat Distiller or ghostscript. The API is very similar and even hypertext functions are supported. There
is also a php extension for pslib in PECL, called ps.

[#22] bmironov at jonview dot com [2003-06-24 15:46:32]

RedHat 9 + Apache 2.0 + PHP 4.3.2 + Oracle 9i + PDFlib 5.0.1 (binary distribution)

It seems to be a working bundle if you do some magic with ./configure:

RedHat 9:
kernel-2.4.20-18.9

Apache 2.0.46:
./configure --enable-so --enable-rewrite=shared --enable-status --enable-mpm=prefork

PHP 4.3.2:
./configure \
--program-prefix= \
--prefix=/usr \
--exec-prefix=/usr \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconfdir=/etc \
--datadir=/usr/share \
--includedir=/usr/include \
--libdir=/usr/lib \
--libexecdir=/usr/libexec \
--localstatedir=/var \
--sharedstatedir=/usr/com \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--without-tsrm-pthreads \    # !!!!!!!!!!!!!!!!!!!!
--with-zlib \
--with-gd \
--enable-gd-native-ttf \
--with-ttf \
--without-mysql \
--with-apxs2filter=/usr/local/apache2/bin/apxs \
--with-oci8 \
--enable-sigchild \
--enable-inline-optimization

Oracle9i:
ln -s $ORACLE_HOME/rdbms/public/nzerror.h $ORACLE_HOME/rdbms/demo/nzerror.h

ln -s $ORACLE_HOME/rdbms/public/nzt.h $ORACLE_HOME/rdbms/demo/nzt.h

ln -s $ORACLE_HOME/rdbms/public/ociextp.h $ORACLE_HOME/rdbms/demo/ociextp.h

If you want to use bundled GD-library then:
1) install following packages: libjpeg, libjpeg-devel, libpng, libpng-devel, freetype, freetype-devel, libtiff, libtiff-devel, zlib, zlib-devel

2) ln -s /usr/lib/libjpeg.so.62 /usr/lib/libjpeg.so
ln -s /usr/lib/libpng.so.62 /usr/lib/libpng.so

It seems to be a working combination, because it is NOT give you:
1) error message in Apache's error_log:
Module compiled with module API=20020429, debug=0, thread-safety=0
PHP compiled with module API=20020429, debug=0, thread-safety=1

2) error message in Apache's error_log:
[notice] child pid 12345 exit signal Segmentation fault (11)

3) MS Internet Explorer can show PDF-output from your PHP-script via Acrobat plug-in and does not crush. No confusing messages about opening "Adobe Acrobat Control for ActiveX".

Hope it will save you some time.

Good luck,
Boris

[#23] pbierans at lynet dot de [2002-03-27 09:56:44]

Load extension, open a PDF, add a font, modify PDF in memory and send
it to browser:

<?php
  
// no cache headers:
  
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
  
header("Cache-Control: no-store, no-cache, must-revalidate");
  
header("Cache-Control: post-check=0, pre-check=0"false);
  
header("Pragma: no-cache");

  
$ext_name="libpdf_php.so";
    
// libpdf_php.so is the PDFLIB for SunOS by "PDFlib GmbH"
    // visit http://www.pdflib.com

  // if the extension is not automatically loaded by Apache
  // dl() will try to load it on demand:
  
if (!extension_loaded($ext_name) && !@dl($ext_name))
  {
    
?>

    <table width="100%" border="0"><tr><td align="center">
      <table style="border: solid #f0f0f0 2px;"><tr>
        <td valign="middle" style="padding: 20px; margin: 0px;">
          <p style="font-family: arial; font-size: 12px; ">
          <b>Sorry,</b><br>
          &nbsp;<br>
          A PDF can not be generated right now.<br>
          The administrator has been informed and will fix this as
          soon as possible.<br>
          Please try again later.
        </p>
      </td></tr></table>
    </td></tr></table>
     <?php
    mail
('admin@domain.com','Error: PDFLib not found',
         
'Called by script:\n  '.$SCRIPT_FILENAME.'?'.$QUERY_STRING,
         
"From: warnings@domain.com\n");
    exit;
  } 
// verify that extension is usable

  // unique serial number:
  
srand(microtime()*10000);
  
$usnrgmdate("Ymd-His-").rand(1000,9999).'-';
  
$pdf_file=$usnr.'result.pdf';
  
$src_file='source.pdf';

  
// create pdf object
  
$pdf pdf_new();
  
pdf_open_file($pdf);
  
pdf_set_parameter($pdf'serial',      'if-you-have-one');

  
// fonts to embed, they are in the folder of this file:
  
pdf_set_parameter($pdf'FontAFM',     'TradeGothic=Tg______.afm');
  
pdf_set_parameter($pdf'FontOutline''TradeGothic=Tg______.pfb');
  
pdf_set_parameter($pdf'FontPFM',     'TradeGothic=Tg______.pfm');

  
// load the source file:
  
$src_doc   =pdf_open_pdi($pdf,$src_file,''0);
  
$src_page  =pdf_open_pdi_page($pdf,$src_doc,1,'');
  
$src_width =pdf_get_pdi_value($pdf,'width' ,$src_doc,$src_page,0);
  
$src_height=pdf_get_pdi_value($pdf,'height',$src_doc,$src_page,0);

  
pdf_begin_page($pdf$src_width$src_height);
  {
    
// place the sourcefile to the background of the actual page:
    
pdf_place_pdi_page($pdf,$src_page,0,0,1,1);
    
pdf_close_pdi_page($pdf,$src_page);

    
// modify the page:
    
pdf_set_font($pdf'TradeGothic'8'host');
    
pdf_show_xy($pdf'Now: '.gmdate("Y-m-d H:i:s"),50,50);
  }
  
pdf_end_page($pdf);
  
pdf_close($pdf);

  
// prepare output:
  
$pdfdata pdf_get_buffer($pdf); // to echo the pdf-data
  
$pdfsize strlen($pdfdata);     // IE requires the datasize

  // real datatype headers:
  
header('Content-type: application/pdf');
  
header('Content-disposition: attachment; filename="'.$pdf_file.'"');
  
header('Content-length: '.$pdfsize);
  echo 
$pdfdata;
  exit; 
// keep this one so no #13#10 or #32 will be written
?>

上一篇: 下一篇: