pdftk unable to save on different folder - php

I'm using pdftk for flattening PDF files on server. For this purpose I use PHP.
This is my code in PHP. Btw, I'm using this on WAMP.
passthru("pdftk editablepdf/jason.pdf output flattenpdf/flattened.pdf flatten");
The pdftk.exe file and the accompanying .dll file both exist in the www directory.
editablepdf and flattenpdf are two separate directories in the www directory.
After executing the script and checking, i find the pdfs haven't been saved.
When i tried running pdftk via command prompt, it worked fine. But the same is not happening here. Is it something that's gotta do with passthru??
Thanks and Regards
Sameer

was able to solve after looking around.
Here's the working line -
passthru("pdftk editablepdf\\jason.pdf output flattenpdf\\flattened.pdf flatten");
Since \ acts as escape sequence character, i'm using "\ \" to denote the folder levels.

Related

How to use tesseract ocr with php from any location

I have installed tesseract in C:\Program Files (x86)\Tesseract-OCR. In command prompt if I am executing with tesseract 123.png sample, then it works. But I can not use tesseract with php. I have tried many libraries and codes for the same.
Someone could help me on this please?
If you needs to use Tesseract in php code, the way to do this is using the http://php.net/manual/en/function.exec.php or http://php.net/manual/en/function.shell-exec.php functions, both allow you to execute bash codes like in "command prompt", but you needs to have more attention to use that, this open several security issues because the client input data sometimes is malicious. Hope it helps!
A working examples:
exec("tesseract C:/your/path/file.png C:/output/file");
Another thing, in part C:/output/file, you don't need to put .txt in output path, tesseract always do the output as .txt file extension.
If the Tesseract is not present on "Environment Variables" the solution is to pass the full executable file path:
shell_exec('"C:/Program Files (x86)/Tesseract-OCR/tesseract.exe" C:/path/to/image C:/output/path/')

Batch converting doc and docx files to txt with utf-8 encoding for middle east languages

I have around 7k files written in Persian with Microsoft word in doc/docx format. I want to convert all of them to txt format without loosing any of them for a php web application. I'm aware of many topics with Converting doc to txt title, but none of them solved my problem. Any idea how can I do it?
Here is a link to one of these files.
Because you couldn't find such a small utility, I created one:
https://github.com/edi9999/docx2txt
to install and use:
npm install docx2txt -g
docx2txt input.docx
However, this won't put spaces between paragraphs.
Maybe you could use pandoc for that too:
pandoc input.docx -o output.txt
(This is one solution, but it might be better done programatically or with a batch file converter.)
Word can convert the files. You need a vba macro that will save a file as UTF-8 text and close it. Then you can use a batch file like the following to open each file and run the macro (e.g., MacroName) on it. (Be sure word security is set to allow macros to run without prompting.)
This batch command runs on every doc and docx file in the current directory and subdirectories. You need to replace the path to Word.
#echo off
for /r %%A in (*.doc*) do call "C:\Program Files (x86)\Microsoft Office\Office14\winword.exe" /q "%%A" /mMacroName

output file directory using ghostscript

I needed to parse a PDF file to images using php. I've done it with the help of ghostscript. Here is the script:
$result = exec("gs -sDEVICE=jpeg -sOutputFile=%03d.jpeg $pdfname.pdf");
But I need the final images to be in another folder. I've tried writing -sOutputFile=/img/%03d or -sOutputFile=img/%03d and in a lot of similar ways...but the program doesn't even work when I add these things.
You need to create the /img/ or ./img directories before you run Ghostscript.
Ghostscript will not automatically create them if they don't exist.

imagemagick convert does not work through php

imagemagick convert does not work through php shell_exec but does through a shell.
In a shell convert file.pdf file.png works fine. But when I have this within a php file as shell_exec('convert file.pdf file.png'); Then I get no output! I have the permissions to do this, so I think it isn't that that's the problem; I have checked the directory I am in by way of getcwd() and this is also ok.
I know shell_exec works because I have used it earlier in the code and that works fine.
Any ideas?
I got the solution thanks to Crontab from another thread. I quote from there:
[I]f you're trying to convert a PDF to a PNG using ImageMagick ...
even using the full path to ImageMagick's convert won't work if the
script's PATH doesn't contain the path location to Ghostscript also.
Without messing with any user paths, you could add:
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Or something similar depending on your setup. The gs executable has
to be in your script user's path somewhere or ImageMagick will fail to
convert PDF or EPS files.
Try the full path to convert, i.e. shell_exec('/usr/bin/convert file.pdf file.png);. You can use which convert to find the location on your system.
There are several reason why this could happen, but I suggest reading this page and the user comments:
http://php.net/manual/en/function.shell-exec.php

Extracting self-extracting exe archive via php

I have a problem. I have the service which is giving me .exe file which they claim is in fact zip archive. A self-extracting archive.
Problem is that I am downloading that with my app (php) to server and need to extract it there witout downloading to local computer.
I have tried download .exe file to local computer - it is self extracting on windows to /temp dir and than self launching FLASH player.
$zip = zip_open($myfile); gives in print_r($zip): 1
zip->open gives no results either.
change .exe to .zip doesn't let winzip or other kind of un-packer on windows to open it - .exe cannot be opened by winzip too.
Now I have no idea how to deal with it. If anybody can advise please.
Try to execute the program as an executable with the system command
Executing files from an external source you don't trust 100% is never a good idea.
The info-zip version of zip allows you to remove the SFX stub from a self-extracting zip file (with the -J flag) converting it back into a normal zip file.
Source code is freely available.
Making a self-extracting zip file is a matter of prepending a zip file with the SFX binary code, then appending the size of the binary stub to the resulting file - but I'm not sure how the data is represented - but a bit of reverse-engineering the available code should make this clear.
Well... if your PHP server is Windows you shouldn't have a problem doing it as a system command. Otherwise, it's a little more tricky. I hear that the unzip system command will unzip self-extracting zip files, but I don't have access to a Linux box at the moment to try it out.
If you're on shared hosting, chances are you can't do it.
Well if you think after executing the exe file, it will extract its content, then you can use exec function to run the .exe files like the one below:
exec("d:\\example\\php\_exe\\1436.exe");
and also you can use system function to run external programs as well.
And also if you wonder what's the difference:
PHP - exec() vs system() vs passthru()

Categories