PDFTK want to transfer to another domain - php

I am working on a project which was developed by another developer
Now client wants me to move it from current linux based server to new windows based server
it is developed in cakephp
and PDFTK is used to fill pdf files dynamically.
I am trying to configure it first on my PC locally, to learn configuration of PDFTK
I have downloaded PDFTK Free version and installed
it generates pdfile with zero bytes size and it is not opeining
I also tried to try in in CMD
pdftk D:\wamp\www\my-project\app\webroot/files/138.pdf fill_form D:\wamp\www\my-project\app\webroot/files/results/1401.fdf output generated_pdfs/test_24.pdf
it returns the following error
Error: Failed to open output file:
generated_pdfs/test_24.pdf
No output created.
Error: unable to open file for output: generated_pdfs/test_24.pdf
while the target folder has permission to write on it

pdftk TEMPLATE_FILE fill_form DATA_FILE output OUTPUT_FILE
try using OUTPUT_FILE with fullpath as below
OUTPUT_FILE = D:/folder/.../generated_pdfs/test_24.pdf

Related

GitHub CI - move a file using PHP copy() in a PHPUnit test

I have an application written in PHP 8. I've added some Unit Tests for it using PHPUnit.
In one of the tests I am using PHP's copy function to move a file from one location to another. This is done to test an endpoint which downloads the file by moving a "dummy" file to the "real" location that the file would be in, in the production application.
My test looks like this:
// tests/TestCase/Controller/DocsControllerTest.php
public function testDownload()
{
$testFile = '75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf';
copy('/full/path/to/testFiles/' . $testFile, '/webroot/docs/');
// Download the file from the endpoint
$id = 9; // File ID to download
$this->get('/download/' . $id);
// This should return a HTTP 200 response containing the PDF
$this->assertResponseCode(200, 'Downloading a valid PDF should produce a 200 response.');
}
To explain the function above:
We have a test file called 75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf. This is a real PDF file with appropriate encoding.
We move the file, using copy(), from a directory where we hold some test files, into the full directory path where the production web application will really store the files (/webroot/docs/).
The remainder of the logic deals with downloading the file from the endpoint. The $this->get makes a HTTP GET request to an endpoint (/download/) which also passes in the appropriate file ID. The location of the file is looked up from a MySQL database and then is streamed to the browser, thus generating a HTTP 200 response containing the PDF.
This works both when I run phpunit locally by executing vendor/bin/phpunit --filter testDownload:
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
Time: 00:05.053, Memory: 20.00 MB
OK (1 test, 16 assertions)
It also works in a browser, i.e. if I make a request to /download/9 I am served the appropriate PDF.
The problem I'm having is on GitHub. When I run the unit test there it fails the CI with this error:
Warning Error: copy(/home/runner/work/my-app/webroot/docs/75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf): Failed to open stream: No such file or directory
In [/home/runner/work/my-app/tests/TestCase/Controller/DocsControllerTest.php, line 745]
Given that this works locally I can't understand why this error is occurring. Is there some restriction with using copy() in GitHub's CI?
The directory and files at /full/path/to/testFiles/ are not .gitignore'd so they are committed with the rest of the repo code. So the test file, 75e57e4a-2149-4270-9d76-c7c8f0298c2c.pdf, exists within the codebase on GitHub.
I am using PHPUnit 9.5.10, PHP 8.0 on a Mac running macOS Monterey (12.2).
It clearly reads:
Failed to open stream: No such file or directory
Maybe add .gitkeep into target directory docs? It may also be, that the source file is not there. One usually can take this error message literal. While it's entirely unclear what $this->get() even is or why your Mac would have anything to do with running GitHub Action?

PHP Laravel - Download returning ZIP file with additional size

I am using Lumen 5.5.
A user may download a zip file that is stored on S3, the PHP will do the following
Download the file from S3 to localhost.
Send the downloaded file to user.
My function is
public function downloadFile(request $request)
{
$localpath = S3Manager::download($request->s3Key);
return response()->download($localpath);
}
The files are being downloaded with additional size and I am not able to unzip it on my Mac with the following error:
Unable to expand file "filename.zip" into "Downloads"
Error 1: Operation not permitted.
That's weird because on the Linux machine (backend) I can unzip it easily. I also checked the Content-Length in the browser, the response returned from the server has the correct length (same as in S3) but the downloaded file size is almost double.
In the frontend I am using js-file-download package
https://www.npmjs.com/package/js-file-download
I'm not sure what is the problem.

Zend-PDF error: Can not open file for writing

I am using ZendFramwork1, my local sever php 5.4. and with MongoDB .I have use function Zend_Pdf to draw PDF in my project. it's run well in my local server. but when i put code to sever that have same version PHP. Everything module is okay but for function print data to pdf is errors . I got message like PDF error: Can not open 'data/reports/myfile.pdf' file for writing. . Any one can help me . what's different between my local server and real sever ?
I am looking to see your reply soon.
thank
First off, chmod 777 is a hack and shouldn't really be used. If you're using apache, you are better off chown'ing the folder to the correct user.

passing parameters to a bat (or vbs) in php

Objective: Use PHP to call a vbs that converts an xls/xlsx file to a csv.
Question: How can I pass a source file path and a destination file path to a vbs that converts xls/xlsx to csv and run that vbs in a PHP web application?
Details: I have a working vbs that takes a source file path and a destination file path and converts the xls/xlsx at source file path into a csv. I can execute it from the Windows cmd line and it does exactly what I want it to do. I can also put the execution command into a bat file and run the bat file to achieve the same results. However, when I use exec()/shell_exec()/system() in PHP to execute the same command no csv is created. (If I try to run the bat from PHP using system() the contents of the bat file show up on the page, in fact, echo Conversion complete! prints "echo Conversion complete! Conversion complete.") I haven't seen any errors yet.
Note: I know about PHPExcel, I'd prefer not to use it.
excelToCsv.vbs
On Error Resume Next
if WScript.Arguments.Count < 2 Then WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>"
Wscript.Quit
End If
csv_format = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.SaveAs dest_file, csv_format
oBook.Close False
oExcel.Quit
batConverter.bat
excelToCsv.vbs conversionTestSourceMS2003.xls batTest.csv
echo Conversion Complete!
index.phtml
<?php
system("cmd /c batConvert.bat")
?>
Note: All of the above files (along with conversionTestSourceMS2003.xls) are in the same directory. I have not implemented any way to pass the parameters (since I can't get it to work even if it's all hard coded...)
Set Up: PHP5, Zend Framework, WAMP, Windows 7 (localhost).
For the sake of simplicity, I merged everything into a single ASP page. This will allow me to hopefully see a similar problem in IIS, and since it is in a single ASP script, I will be able to see the error more directly. My test machine is running on Windows Vista SP2 on IIS7 with Excel 2007 SP3.
excelToCsv.asp
<%
Option Explicit
Dim csv_format, src_file, dest_file, strPath, objFSO
csv_format = 6
src_file = "conversionTestSourceMS2003.xls"
dest_file = "testbat.csv"
strPath = "[HARDCODED PATH HERE]\"
src_file = strPath & src_file
dest_file = strPath & dest_file
Dim objExcel, objBook
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open(src_file)
objBook.SaveAs dest_file, csv_format
objBook.Close False
Response.Write "Conversion Complete!"
objExcel.Quit
%>
When running this code, I got a generic ASP error. So, I enabled detailed error messages in ASP and I get this following error...
Microsoft Office Excel error '800a03ec'
Microsoft Office Excel cannot access the file '[HARDCODED PATH
HERE]\conversionTestSourceMS2003.xls'. There are several possible
reasons: • The file name or path does not exist. • The file is being
used by another program. • The workbook you are trying to save has the
same name as a currently open workbook.
/temp/ExcelToCsv.asp, line 18
Now, this is not Apache, but I do believe the problem is related to yours. This error implies there is a security/permission problem where Excel cannot do what it needs to do to access or read the file. In fact, I encountered similar errors when I was executing the VBScript (and passing the error up the chain) from PHP (in IIS).
I believe it can be resolved by changing the Windows User being used to create the process. This can be configured in Services.msc by editing the Apache service and changing the Log On tab to an actual Windows user instead of a Service Account. I have not tested it yet, though, since setting up Apache is not something I can do right now.

system call to gnuplot from php to generate plot?

So, I am using a php program to read a file, make some changes and then write it to a new file. After that, I call gnuplot, using a system call:
system('cat sarx.conf | /usr/bin/gnuplot');
sarx.conf has the gnuplot commands to generate the plot. The problem is if run my php from the command line (its on a linux server) it generates the image and stores it on the disk. But when I do the same thing by running the php on my browser it generates the image and tries to spit it out on the browser without actually storing it on disk.
Things i tried:
I though i might have had issues with permission settings but it didn't help.
I also hard coded the path where I want the image to be in sarx.conf. That didn't help either.
I also tried looking for it in the tmp directory --- no luck!!
Does anyone have any ideas on how can I get this to work? I need to store this image on disk so that my website can grab it to show the plot later. Is there any php stuff which can grab the image and write it to disk?
There is a great LGPL-licensed PHP interface to gnuplot here: http://www.liuyi1.com/PHP-GNUPlot/
Here is how you could do something similar:
$my_file = tempnam();
$handle = popen('gnuplot', 'w');
fwrite($this->ph, "Run some gnuplot commands here\n");
fwrite($this->ph, "set term png\n");
fwrite($this->ph, "set output ".$my_file."\n");
fwrite($this->ph, "replot\n");
flush($handle);
pclose($handle);
header('Content-Length: '.filesize($my_file));
header('Content-Type: image/png');
print file_get_contents($my_file);
unlink($my_file);

Categories