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
Related
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;
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);
}
I use this code to retrieve and display an image:
header("Content-type: image/png");
echo file_get_contents(site_domain().image_asset_module_url("header.png",$this->name));
on my local WAMP it works, but on the remote server file_get_contents returns a wrong-encoded string:
Local:
‰PNG IHDR^jRÀ2¡ pHYsÒÝ~üÿÿIDATxÚ콘Uõµþ¿`ŠŠÔéÃÕ¨¹&&ù'77¹i¦˜è‰=V:RlH‡™aAlH™B¯Jbh...
Remote:
�PNG IHDR^jR�2� pHYs��~���IDATx����U����`������ը�&&�'77�i��草=V:Rl...
If I use utf8_encode I get:
PNG IHDR^jRÀ2¡ pHYsÒÝ~üÿÿIDATxÚì½Uõµþ¿`ÔéÃÕ¨¹&&ù'77¹i¦è=V:RlHaAlHB¯Jbh...
So I always get a break picture on my remote Server - why and what is the solution?
The data is always the same. file_get_contents does not alter data in any way. You're also not dealing with text in some encoding, but with binary data. Any sort of text-encoding or conversion thereof does not apply here.
Your first sample is the binary image data as interpreted as Latin-1 encoded text.
Your second sample is the same binary data as interpreted as UTF-8 encoded text.
I.e., the data is fine, the interpretation is wrong. The interpretation should be set by the Content-Type header, perhaps this is not being set correctly on the remote server. For this problem, inspect the raw HTTP response headers and see How to fix "Headers already sent" error in PHP.
I would have rather used
<?php
$file = 'http://url/to_image.png';
$data = file_get_contents($file);
header('Content-type: image/png');
echo $data;
Or Can you try this
$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: $imginfo['mime']");
readfile($remoteImage);
I have been making a program that creates multiple CSV's from another source CSV (encoded in 'SJIS'/SHIFT-JIS). Here's the process in which I am creating them:
Create a string, which will hold the contents of the output CSV's
Fill in said strings with their proper information
Encode the string to UTF-8 from SJIS using mb_convert_encoding()
code:
$contents2 = mb_convert_encoding($contents, "UTF-8", "SJIS");
Create a zip archive using PHP's provided library methods and append the files I desire with their corresponding strings using addFromString()
code:
$zipFileName = "output.zip";
$zip = new ZipArchive;
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE){
$zip->addFromString('customer.csv', $contents2);
...do for the other files
$zip->close();
}
else{
echo 'Failed! File not created!';
}
Prompt the user with a dialogue box to save the file in their desired location.
code:
$zipContents = file_get_contents($zipFileName);
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=inflow.zip");
header("Pragma: no-cache");
header("Expires: 0");
echo $zipContents;
Now here is my problem: The files that I have created from the zip file are encoded in "UTF-8 without BOM" when I open it in Notepad++. However, I require for these files to just be in plain "UTF-8". A inventory program I am using to upload these files, for reasons beyond me, will not show the proper characters for the CSV's encoded in "UTF-8 without BOM". Once I manually: open the files, re-encode it as "UTF-8", and save them, are the files able to display the correct characters in this inventory program.
I have read a good deal of articles talking about the converse of this problem, where people were seeking to make their UTF-8 files become encoded without BOM. However, my situation is the exact opposite of this. If there's an easy solution in PHP I would more than welcome the help! Thanks for reading!!
Found a possible solution, see this: How can I output a UTF-8 CSV in PHP that Excel will read properly?
I'm retrieving data from my Postgres DB in UTF-8. The db and the client_connection settings are in UTF-8.
Then I send 2 headers to the visitor:
header("Content-Type: application/msexcel");
header("Content-Disposition: $mode; filename=export.xls");
and start outputting plain text data in a CSV-manner. This will open as a simple Excel file on the visitors desktop.
$cols = array ("col1", "col2", "col3");
echo implode("\t", $cols)."\r\n";
Works fine, untill special characters like é, è etc are encountered.
I tried changing my client_encoding while retrieving the data from the db to latin-1, which works in most cases but not for all chars. So that is not a solution.
How could I send the outputted file as UTF-8? I don't think converting the data from the db to latin-1 is possible, since the char seems unknown in latin-1 ... so I need Excel to treat the file as UTF-8
I'd look into using the PHPExcel engine. It uses UTF-8 as default and it can generate a whole list of spreadsheet file types (Excel, OpenOffice, CSV, etc.).
I would recommend not sending plain-text and masquerading it as Excel. XLS files are typically binary, and while binary isn't required, the official Excel method of using non-binary data is to format it as XML.
You mention "CSV" in the title, but nothing about your problem includes anything related to CSV. I bring this up because I believe that you should actually change your tabs to commas, and then you could simply output a standard .csv file, which is read by Excel still but doesn't rely on undocumented or unstable functionality.
If you truly want to send application/msexcel, then you should use a real Excel library, because currently, you are not creating a real Excel file.
use ; charset=UTF-8 after aplication/xxxxxx I do use:
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
// header("Content-Length: " . strlen($thecontent)); // this is not mandatory
header('Content-Disposition: attachment; filename="file.xls"');
Try mb_convert_encoding function.
Try to use iconv, for converting string into required charset.
Have you tried utf8_encode() the string?
So something like: echo implode("\t", utf8_encode($cols)."\r\n")
Not sure if that would work, but give it a go