PDFLib TET PHP: Can`t extract images - php

I was able to install TET (php_tet.dll) on Windows 8.1 + Xampp and I have no problems with PDF to Text, but I had no luck with image extraction.
I'm using the example "image resources.php" ( and "image_extractor.php" ) which is supposed to "print" some info about the images (x, y, width, height, alpha and e.t.c) in PDF file. Also must save/extract all available (or any) images into files (tiff, jpg).
The examples can be found here: http://goo.gl/ZeDlc0
The part with image information is working, but there is no files extracted.
I haven't got any trouble with text extraction to TXT file in the same folder.
So I'm able to write there ?
Is something wrong with my SEARCHPATH or else ?
My TRY:
The original example throws ERROR:
Error 1016 in open_document(): Couldn't open PDF file 'FontReporter.pdf' for reading (file not found)
So I changed the SEARCHPATH:
/* global option list */
$globaloptlist = "searchpath={{../data} {../../data} }";
with the location of my pdf file:
/* global option list */
$globaloptlist = "searchpath={{D:\Workshop\www\TET\data} }";
Now I have some output data via print/echo:
page 7: 208x277pt, alpha=0, beta=0 id=0, 595x750 pixel, 1x8 bit Indexed
page 7: 208x277pt, alpha=0, beta=0 id=1, 595x750 pixel, 1x8 bit Indexed
The $tet->write_image_file method returns 10 which says "I can extract TIFF file".
But no images are extracted in my pdf`s folder or anywhere around...

Somehow the images are exported in D:\workshop\xampp\apache
In the option FILENAME I need to set the ABSOLUTE path and the filename...
$path = str_replace('\\', '/', __DIR__);
$imageoptlist = $baseimageoptlist . " filename {".$path."/out/" .
$outfilebase . "_p" . $pageno . "_I" . $ti->imageid . "}";
if ($tet->write_image_file($doc, $ti->imageid, $imageoptlist) == 0){
print("Error " . $tet->get_errnum() . " in " .
$tet->get_apiname() . "(): " . $tet->get_errmsg());
}

this is exactly what I found in the TET manual, (chapter 3.9 "PHP" section):
File name handling in PHP
Unqualified file names (without any path component) and relative file names are
handled differently in Unix and Windows versions of PHP:
- PHP on Unix systems will find files without any path component in the directory
where the script is located.
- PHP on Windows will find files without any path component only in the directory
where the PHP DLL is located.
So I guess, it's expected that you have to adjust the sample slightly for your needs.

Related

phpmailer attaching two pdf files

I am using phpmailer for attaching pdf files and sending email with pdf attachments. One pdf file is being attached while the other is not being attached.
I am using the code as
$attachedfile = $_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_form_test.pdf';
$mail->addAttachment($attachedfile, 'Visa Application');
$attachedfile2 = $_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf';
$mail->addAttachment($attachedfile2, 'Visa Application 2');
Only one pdf file is being attached other one is being attached.
It also works with single pdf file attatchment.
I have also use the following code
$attachedfile = array($_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf',$_SERVER["DOCUMENT_ROOT"] . '/wp-content/plugins/xyz-user-registration/images/iraq_visa_test.pdf');
foreach($attachedfile as $attachment){
$mail->AddAttachment($attachment);
}
But again it attachs one pdf file
please help
You mentioned that PHP returns 1 and nothing for the two calls to addAttachment. That's what PHP uses as text representation of true and false: One of your files is not readable by PHP, because the file is missing, the path is wrong, or it lacks sufficient ownership or permission. Double-check your paths and permissions.
When I say don't build your paths, I mean use only literal strings for the paths. You can write a standalone PHP script to check them:
<?php
$path1 = '/var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form_test.pdf';
$path2 = '/var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf';
var_dump($path1, is_file($path1), $path2, is_file($path2));
Check them in your shell too:
ls -al /var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form_test.pdf /var/www/mysite/wp-content/plugins/xyz-user-registration/images/iraq_visa_form.pdf
If those are OK, go back to your original script and var_dump your generated paths, and compare them - including the length, in case you've accidentally included some non-printing or zero-width chars.

PHP saves broken image files downloaded from a URL

No matter which function I'm using:
copy("http:" . $imglink, "images/" . substr($imglink, 34));
//or
file_put_contents("images/" . substr($imglink, 34), file_get_contents("http:" . $imglink));
//or
file_put_contents("images/" . $productData['imagefile'], fopen($productData['imagelink'], 'r'));
the files are saved broken and almost 4 times bigger. No errors in the log,
already checked that I can manually download healthy images from the remote server through the browser. Any ideas?
Found the problem - the image filename contains space char which should be
rawurlencode($imglink)-ed before it is passed as argument

"No such file or directory" on localhost copy

EDIT: I'm pretty sure the issue has to do with the firewall, which I can't access. Marking Canis' answer as correct and I will figure something else out, possibly wget or just manually scraping the files and hoping no major updates are needed.
EDIT: Here's the latest version of the builder and here's the output. The build directory has the proper structure and most of the files, but only their name and extension - no data inside them.
I am coding a php script that searches the local directory for files, then scrapes my localhost (xampp) for the same files to copy into a build folder (the goal is to build php on the localhost and then put it on a server as html).
Unfortunately I am getting the error: Warning: copy(https:\\localhost\intranet\builder.php): failed to open stream: No such file or directory in C:\xampp\htdocs\intranet\builder.php on line 73.
That's one example - every file in the local directory is spitting the same error back. The source addresses are correct (I can get to the file on localhost from the address in the error log) and the local directory is properly constructed - just moving the files into it doesn't work. The full code is here, the most relevant section is:
// output build files
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
copy($source, $dest);
echo "Copy $source to $dest. <br>";
}
}
You are trying to use URLs to travers local filesystem directories. URLs are only for webserver to understand web requests.
You will have more luck if you change this:
copy(https:\\localhost\intranet\builder.php)
to this:
copy(C:\xampp\htdocs\intranet\builder.php)
EDIT
Based on your additional info in the comments I understand that you need to generate static HTML-files for hosting on a static only webserver. This is not an issue of copying files really. It's accessing the HMTL that the script generates when run through a webserver.
You can do this in a few different ways actually. I'm not sure exactly how the generator script works, but it seems like that script is trying to copy the supposed output from loads of PHP-files.
To get the generated content from a PHP-file you can either use the command line php command to execute the script like so c:\some\path>php some_php_file.php > my_html_file.html, or use the power of the webserver to do it for you:
<?php
$hosted = "https://localhost/intranet/"; <--- UPDATED
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$path = str_replace("\\","/",$path); <--- ADDED
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
$content = file_get_contents(urlencode($source));
file_put_contents(str_replace(".php", ".html", $dest), $content);
echo "Copy $source to $dest. <br>";
}
}
In the code above I use file_get_contents() to read the html from the URL you are using https://..., which in this case, unlike with copy(), will call up the webserver, triggering the PHP engine to produce the output.
Then I write the pure HTML to a file in the $dest folder, replacing the .php with .htmlin the filename.
EDIT
Added and revised the code a bit above.

Add contents to ZIP without full path not working

I am creating Epub package which I need to pack in the following order
$dest = "ebooks/pack/";
exec("zip -DX0 ".$dest."/book.zip ".$dest."/mimetype ");
exec("zip -DrX9 ".$dest."/book.zip ".$dest."/META-INF ".$dest."/OEBPS");
So I when I pack this , Zip archive structure looks like below
ebooks/pack/mimetype
ebooks/pack/META-INF
ebooks/pack/OEBPS
But I need that files to be in home , For example when I un compress It should look like below
mimetype
META-INF
OEBPS
I also tried to navigate via cd ebooks/pack/ , that throws the error
zip warning: name not matched: mimetype
I even used full path like home/user/public_html/ful
exec("cd home/sam/public_html/domain.com/".$dest."/; zip -DX0 ".$dest."/book.zip mimetype 2&1",$output);
I even tried chdir , It displays the correct path where my files are there
echo getcwd() . "\n";
chdir('home/sam/public_html/domain.com/".$dest."/');
echo getcwd() . "\n";

Check for file with same filename, but different extension

I have a directory contain jpeg and raw image files. Some jpeg files have a raw file version of them, some don't. Luckily, if a jpeg has a raw file they are named the same (excluding the extension). So, I need a way to check this directory for a matching raw file of the same filename, exclusing file extesion. the raw file, file extension could be pretty much anything.
Any ideas how I can do this? I have the filename (excluding extesion) stored of $filename at the moment.
To explain further. I have a directory with the following files in it:
cat.jpg
dog.jpg
bird.jpg
cat.raf
dog.foo
I need to match cat.jpg to cat.rag and dog.jpg to dog.foo. These have just been extracted from a uploaded zip file.
Try searching for files starting with the same name:
$fileWithoutExtension = basename($filename, '.jpg');
$allFilesWithThisName = glob($fileWithoutExtension . '.*');
if (count($allFilesWithThisName)) {
echo 'There is another file with this name';
}
As you already have the filename w/o the extension, you can just check if the raw file exists (file_exists()):
if (file_exists($filename.'.raw')) {
echo 'RAW file exists:', $filename , "\n";
}
But this seems so trivial to me, that I might did not understood your question completely.

Categories