PHP Convert byte array as string into pdf - php

I have a byte array in string format in php from SOAP Webservice , want to convert to pdf .
Web service Response Data
<RunProcessResponse IsReport="true" ReportFormat="pdf" xmlns="http://ie.org/ADInterface/1_0">
<Data>255044462D312E350A25E2E3CFD30A342030206F626A0A3C3C2F436F6C6F7253706163652F4465766963655247422F537562747970652F496D6167652F486569676874203134372F46696C7465722F4443544465636F64652F547970652F584F626A6563742F5769647468203132342F42697473506572436F6D706F6E656E7420382F4C656E67746820383333303E3E73747265616D0AFFD8FFE000104A46494600010101006000600000FFE1006C45786966000049492A0008000000030012010300010000000100000031010200070000003200000069870400010000003A00000000000000476F6F676C650000030000900700040000003032323002A00400010000007C00000003A00400010000009300000000000000FFDB0043000201010201010202020202020202030503030303030604040305070607070706070708090B0908080A0807070A0D0A0A0B0C0C0C0C07090E0F0D0C0E0B0C0C0CFFDB004301020202030303060303060C0807080C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0CFFC00011080093007C03012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131</Data>
</RunProcessResponse>
PHP Code:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="service.pdf"');
echo base64_decode($byte_string);
Webservice return pdf response in JAVA
Java Code :
file_type ="pdf";
RunProcessResponse r= res.addNewRunProcessResponse();
r.setData(java.nio.file.Files.readAllBytes(pi.getPDFReport().toPath()));
r.setReportFormat(file_type);
Please help me to resolve this.

A quick test suggests the string is encoded as Base16 (hexadecimal) (an odd choice).
PHP has a handy function available in hex2bin()
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="service.pdf"');
echo hex2bin($byte_string);
exit;

Related

XAMPP, PHP: echo adds two spaces to blob read from database

I read blob (word 2010 document) from mysql database and store it in $data variable. When I simply store that data directly in PHP like so:
file_put_contents('c:\\temp\\dump.docx', $data);
I can open dump.docx in Word (size matches original file). If I attempt to send $data like this:
ob_start();
header('Content-disposition: attachment; filename=' . $name);
header('Content-type: ' . $type);
ob_clean();
echo $data;
ob_end_flush();
exit;
The stored file is two bytes longer. There are two spaces in front:
To check if I somehow do not output those spaces, I called ob_get_contents() just before echo and dumped content to a file. File has zero bytes.
So it looks like echo is producing those two bytes.
Here's post that helped me:
https://drupal.stackexchange.com/questions/163628/extra-space-at-beginning-of-downloaded-image/163644
ob_start was already called ealier. I needed to call only ob_clean() before sending content.

How to export data in CSV into DOS\Windows Format in PHP?

I have an export in PHP like this :
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="agents_list.csv"');
When I make export, Notepad++ inform me that the format is "Macintosh" (CR). I need to have it in "Dos\Windows" (CR+LF) format.
How can I do that ? Must I modify some header ? Thank you for your help.
I replaced \r == CR (aka Mac style) with \r\n that is DOS format. Thank you #Andrey for your suggestion that makes me in way to find the solution.

Make PHP exported CSV with UTF-8 character work on mac excel using commas

So I'm trying to export a csv using PHP in which the contents contains UTF-8 character and I want the resultant csv to open in Excel smoothly (including Mac excel)
So there is an answer here: How can I output a UTF-8 CSV in PHP that Excel will read properly?
Checkout the top answer.
But then in order to implement that you need to use tabs to separate the fields instead of commas...Is there a way to achieve this while still using commas and not tabs and still have it work in OS X
EDIT
Mostly to Mark Baker but everyone feel free to comment
Another code update
while(#ob_end_clean());
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=fileexport.csv");
echo "\xEF\xBB\xBF";
print "sep=,\n";
print $output;
exit;
fputcsv should work fine in this instance. Take the following example, where as the third parameter of fputcsv is the delimiter. By default it is , (comma), but you could also use "\t" for tab files. CSV files should be interpreted the same on either OS
if( $fh = fopen("output_file.csv","w") ){
$put = array("column1, with comma","column2, with comma","column3" /*,"columnN"*/);
fputcsv($fh,$put,",");
fclose($fh);
}

Hexadecimal String to binary PDF file

I have a hexadecimal value that is a PDF that I am getting from a web service that I am trying to save locally using PHP. The below is a snippet of the value.
I have tried to achieve this using pack in PHP either receive an error that "x" is not valid or the pdf will not save correctly. It will be empty or says error opening.
Partial Value is: "0x255044462D312E340A0A322030206F626A0A3C3C2F5479"
I have tried the following unsuccessfully after searching google for some time:
$pack = pack("h*", $string);
file_put_contents('my.pdf', $pack);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $pack;
Can anyone tell me what I am doing incorrectly? I realize this is not the entire PDF but I cant put entire online.
Thanks for any help

PHP import CSV with utf-8 accents

I am having issues importing a CSV file which contains (french) names with accents in them... when ever they are imported the accent do not display properly example
félix turns into fŽlix
the file is created by hand and then imported into PHP.
I have tried both utf8_encode() and utf8_decode() and nether function will convert the chars so they can be viewed properly.
my question is how can i get this to render properly... convert char-set.. etc
I believe the text is encoded in Cp850 based on other questions i've seen on here. I am using fgetcvs() to get the contents.
Set Header Information before you output as UTF
header('Content-Type: text/html; charset=utf-8');
$log = file_get_contents("log.csv");
echo utf8_encode($log);
Output
félix
Please, try iconv() function
I think this is late answer but may be helpful for those who are still searching for solution. This is just a tweak. Not always recommended .
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=filename.csv');
echo "\xEF\xBB\xBF"; // UTF-8 with BOM
readfile("filename.csv");
exit;
I'm doing this on upload
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir .$target_file)) {
$log = file_get_contents($target_dir .$target_file);
file_put_contents($target_dir .$target_file, utf8_encode($log));

Categories