4

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

8
  • Remove 0xusing str_replace. Commented May 21, 2013 at 23:18
  • Thanks for the response. I have tried that as well.. I then get a file to download but it cannot open it. Commented May 21, 2013 at 23:21
  • I know what's going on, it's not pack('h*',$string) it's pack('H*',$string), with uppercase. Commented May 21, 2013 at 23:24
  • Changed to below. Still says cannot be opened may be damaged. This same one opens in an application fine.. So I know it is valid. $string = str_replace("x", "", $string); $pack = pack("H*", $string); file_put_contents('my.pdf', $pack); header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="my.pdf"'); echo $pack; Commented May 21, 2013 at 23:30
  • It's str_replace("0x", "", $string); :P Commented May 21, 2013 at 23:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.