I am using Pdfparser Library for parsing pdfs. While parsing, Some pages of the 20-page pdf file are read and some pages are not. This is code I am using
$str_path = 'example_book.pdf';
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile($str_path);
$pages = $pdf->getPages();
$page = $pages[7];
$text = $page->getText();
echo $text;
When I run the php script I get this error:
Call to undefined method Smalot\PdfParser\Encoding::__toString()
Smalot\PdfParser\Font::translateChar
vendor/smalot/pdfparser/src/Smalot/PdfParser/Font.php:104
Is there any other way to do this?
follow below steps
1.open below mentioned file on the editor
vendor\smalot\pdfparser\src\Smalot\PdfParser\Font.php
search for function
translateChar
3. Comment Out the below-mentioned code (or remove )
if (\strlen($char) < 2 && $this->has('Encoding') && 'WinAnsiEncoding'
=== $this->get('Encoding')->__toString()) {
$fallbackDecoded = self::uchr($dec);
}
save file and exit.
Related
I'm using pdfparser to parse text from a pdf file. for old version pdf files it is working but for new version pdf files this parser is not working.
my pdf version is 1.7
<?php
include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('sample.pdf');
// Retrieve all pages from the pdf file.
$pages = $pdf->getPages();
// Loop over each page to extract text.
$content=array();
foreach ($pages as $page) {
$content[]= $page->getTextArray();
echo"<pre>";
print_r($content);
}
I experienced the same behaviour!
Now I use a tool to check the pdf version before I try to parse it. If it is not 1.4 I convert it to 1.4 and parse it then.
Here is a php library for that if needed: https://github.com/xthiago/pdf-version-converter
Code example:
function searchablePdfParser($systemPath) {
//we save the file to a temporay file because we might need to convert it.
$tempPath = getPathWithIdAndTimestamp($systemPath) . 'tmp.pdf';
copy($systemPath, $tempPath);
//check whether it needs to be converted and convert it if required
$guesser = new RegexGuesser();
$pdfVersion = $guesser->guess($tempPath); // will print something like '1.4'
if ( $pdfVersion != '1.4' ) {
$command = new GhostscriptConverterCommand();
$filesystem = new Filesystem();
$converter = new GhostscriptConverter($command, $filesystem);
$converter->convert($tempPath, '1.4');
}
//parse the original file or the converted file if it hadn't been a pdf 1.4 version
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile($tempPath);
$text = $pdf->getText();
unlink($tempPath);
if ( strlen($text) < 30 ) {
return '';
}
return $text;
}
I'm using the getID3 library to get the details of a remote video file. I'm trying to read a portion of the file to get the details of the file, however some videos don't have the full details at the start.
For these videos, I'm trying to download the full video, and then extract the relevant information. However, even after the video has downloaded completely, getID3->analyze($filename), returns the same erroneous file info.
But when I copy the video, and then run the function analyze($filename.'copied.mp4') on copied video, it returns the correct info even though the file contents are same. Perhaps getID3 isn't loading the video again, however, how can I fix this issue without copying the video.
Please find the code below.
if ($fp_remote = fopen($remotefilename, 'r')) {
echo 'conn opened';
$localtempfilename = tempnam('/home/xerox/abc', 'whateva').'.mp4';
if ($fp_local = fopen($localtempfilename, 'wb')) {
$count = 0;
$countExpiry = 8;
while ($buffer = fread($fp_remote, 8192)) {
$count++;
fwrite($fp_local, $buffer);
if ($count >= $countExpiry) {
fflush($fp_local);
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($localtempfilename);
if ($ThisFileInfo["error"]){
print "problem encouterd";
$countExpiry += 1000;
} else {
break;}
}
}
fclose($fp_local);
$getID31 = new getID3;
copy ( $localtempfilename, $localtempfilename.'_copied.mp4' );
$ThisFileInfoz = $getID31->analyze($localtempfilename.'_copied.mp4');
// Delete temporary file
unlink($localtempfilename);
fclose($fp_remote);
var_dump($ThisFileInfoz);
}
}
A call to clearstatcache solved the problem for me,
since repeated calls to things like filesize will be cached by the
filesystem and getID3 won't read beyond end-of-file.
source: James Heinrich, developer of getID3.
I want to show odometer simple example but its not working. My jpgraph files are within src/ directory. Myline graph is working but for odometer which file to use ??
PHP code:
<?php
require_once ('/src/jpgraph.php');
require_once ('/src/jpgraph_line.php');
require_once ('jpgraph/jpgraph_odo.php');
// Create a new odometer graph
$graph = new OdoGraph(500,180);
$odo = array();
// Now we need to create an odometer to add to the graph.
for( $i=0; $i < 5; ++$i ) {
$odo[$i] = new Odometer();
$odo[$i]->SetColor('lightgray:1.9');
$odo[$i]->needle->Set(10+$i*17);
$odo[$i]->needle->SetShadow();
if( $i < 2 )
$fsize = 10;
else
$fsize = 8;
$odo[$i]->scale->label->SetFont(FF_ARIAL,FS_NORMAL,$fsize);
$odo[$i]->AddIndication(92,100,'red');
$odo[$i]->AddIndication(80,92,'orange');
$odo[$i]->AddIndication(60,80,'yellow');
}
// Create the layout
$row1 = new LayoutHor( array($odo[0],$odo[1]) );
$row2 = new LayoutHor( array($odo[2],$odo[3],$odo[4]) );
$col1 = new LayoutVert( array($row1,$row2) );
// Add the odometer to the graph
$graph->Add($col1);
// ... and finally stroke and stream the image back to the browser
$graph->Stroke();
?>
Error:
Fatal error: Class 'OdoGraph' not found in C:\wamp\www\PowerMonitor\odometer.php on line 9
I had similar problem of this Fatal error. As Gal pointed out, it is problem with in-proper JPGRAPH_ODO.PHP loading or missing. You can download the file from here and keep it under src directory with other files. Then apply
require_once ('/src/jpgraph_odo.php');
might work.
I am using google drive sdk for uploading files and also I think i can possibly create pdf file on it - so I tweak the code. So in my code, I generate HTML text format to be used as content for the PDF to be created in google drive.
this is a snip-it of my code.
$subcontent = "<h1>Hello World</h1><p>some text here</p>";
$file = new Google_DriveFile();
....
$mkFile = $this->_service->files->insert($file, array('data' => $subcontent));
$createdFile = $fileupload->nextChunk($mkFile, $subcontent); // I got error on this line
$this->_service->files->trash( $createdFile['id'] );
...
when I run the code I got an error based the comment I put in my code above:
Catchable fatal error: Argument 1 passed to Google_MediaFileUpload::nextChunk() must be an instance of Google_HttpRequest, array given, called in /home/site/public_html/mywp/wp-content/plugins/mycustomplugin/functions.php on line 689 and defined in /home/site/public_html/mywp/wp-content/plugins/mycustomplugin/gdwpm-api/service/Google_MediaFileUpload.php on line 212
I have no idea what should be the value in the 2nd parameter in nextChunk, in the original code it goes like this:
.....
$handle = fopen($path, "rb");
while (!$status && !feof($handle)) {
$max = false;
for ($i=1; $i<=$max_retries; $i++) {
$chunked = fread($handle, $chunkSize);
if ($chunked) {
$createdFile = $fileupload->nextChunk($mkFile, $chunked);
break;
}elseif($i == $max_retries){
$max = true;
}
}
.....
my question is that, how can I deal with this error? and how can I successfully create pdf file in google drive by tweaking this code? because I need the created file ID of the file to be linked in my post.
Thanks in advance...
You shouldn't be passing $mkFile to nextChunk. Have a read though the resumable uploads section in the documentation.
You have more fundamental issues than that though. For instance, the insert call you do already sets the data for the file. There is no need to do anything with nextChunk unless you are doing resumable uploads. Follow the "Multipart File Upload" section of the above doc to fix your insert statement and just remove the nextChunk line.
I'm trying to take advantage of Wordpress' 3.6 support for getID3(), but I'm having trouble writing ID3 tags to my mp3s. Here is the code I'm using:
if ( ! class_exists( 'getID3' ) ) {
require(ABSPATH.WPINC.'/ID3/getid3.php' );
}
# I had to custom install the write library because WP doesn't include it by default, evidently
getid3_lib::IncludeDependency(TEMPLATEPATH.'/lib/getid3/write.php', __FILE__);
$testfile = "/Users/jplew/Sites/dev.example.com/content/uploads/mp3.mp3";
$tagwriter = new getid3_writetags;
$tagwriter->filename = $testfile;
$tagwriter->tagformats = array('id3v2.4');
$TagData['title'][] = 'My Heart Will Go On';
$TagData['artist'][] = 'Celine Dion';
$TagData['genre'][] = '90s Classics';
$tagwriter->tag_data = $TagData;
if ($tagwriter->WriteTags()) {
echo 'success';
} else {
echo 'failure';
}
I am consistently getting 'failure'. The path to the getid3_writetags function is fine. I know this because when I print_r($tagwriter);, it outputs all the arrays as specified. However the 'warnings' and 'errors' arrays are both empty.
Moreover, when I do the following it successfully returns all the correct tag information:
$data = $getid3->analyze( $testfile );
print_r($data)
I should also note that the demo.write.php included in the demos also fails when I run it. It gets as far as starting to write tag(s) then stops.
Any ideas? I'd post on the getid3.org forum but my IP is blacklisted. :(
Wordpress did not include the entire GetID3 library when they implemented it. They only used the files to get the most popular video and audio tags.