1

I have a PHP script that creates large complex tables. I am attempting to set up a shell script which would accept an ID as an argument and then run the PHP script using the ID and accept the HTML output of the PHP script for use as part of a cURL post to DocRaptor to make a PDF.

Example shell script looks like this and I want the document_content to be my generated HTML.

myHTML = usr/bin/php mytablemaker.php?id=$1
curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"true", "document_content":"myHTML"}}' http://docraptor.com/docs > docraptor_sample.pdf

How do I do this correctly?

jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139

3 Answers3

2

If that is bash, something like this should work:

myHTML = $(usr/bin/php mytablemaker.php?id=$1)
curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"true", "document_content":"'"$myHTML"'"}}' http://docraptor.com/docs > docraptor_sample.pdf

However you don't ask for HTML but for HTML as a json string, so make you PHP script encode the string as json, see json_encode. Or do a addcslashes($output, '"') on the " characters.

See as well:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks hakre! The examples from DocRaptor don't encode the HTML AFAICS. Their example sh is here: https://raw.github.com/gist/1045686/5173a5a148eabe6065a67b85018f41a15f099045/doc_raptor_example.sh – jerrygarciuh Jun 27 '12 at 22:43
  • They do, see what I mean by taking a look how a string in json works: http://json.org - If the PHP script outputs HTML that contains a `"` for example and it's not escaped, the remove service will think that the `document_content` string ends there - if not even the whole json is invalid and it fully refuses to operate (!). – hakre Jun 27 '12 at 22:45
  • Heh, that generated a PDF with content: `usr/bin/php reports_scorecard_blank_PDFMAKER.php?id=$1` I'll try without quotes – jerrygarciuh Jun 27 '12 at 22:55
  • Yes quotes were wrong. If you use json_encode inside the PHP file, you don't need the outer quotes around the HTML as well, because they are part of it. – hakre Jun 27 '12 at 22:56
  • Calling PHP interpreter like this will most certainly lead to a following error: Could not open input file: mytablemaker.php?id=1 You can't pass GET variables to the command line PHP interpreter, unless you use the CGI version. – anttix Jun 27 '12 at 22:58
  • Hmm, altered table maker to do output like so: json_encode(ob_flush()); and removed quotes in cURL call but my PDF content remains `usr/bin/php mytablemaker.php?id=$1` – jerrygarciuh Jun 27 '12 at 23:01
  • Hmm maybe `usr/bin/php mytablemaker.php?id=$1` does not execute? also `json_encode(ob_flush());` looks really wrong. Take a look at [`ob_get_clean`](http://php.net/ob_get_clean) instead of the flush. – hakre Jun 27 '12 at 23:03
  • Also if you try to assign to a variable in SH like this a = "value" you will get a: command not found. – anttix Jun 27 '12 at 23:07
  • Error message helped me out! They also accept a document_url argument not shown in examples! Have it working now. Thanks! – jerrygarciuh Jun 27 '12 at 23:18
2

The best way is to modify that mytablemaker.php to take the command line use case into account. e.g. like this:

if(isset($argv[1])) {
    $id=$argv[1];
} else {
    $id=$_GET["id"];
}

Then from BASH you do:

# Get HTML from PHP script and escape quotes and
# backslashes in string to comply to the JSON specification 
myHTML=$(/usr/bin/php -f mytablemaker.php $1 | sed -e 's/[\\"]/\\&/g')

# Put the value of myHTML in a JSON call and send it to the server
curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"true", "document_content":"'"$myHTML"'"}}' http://docraptor.com/docs -o docraptor_sample.pdf

Note the string concatenation done at the last line:

'first part'"second part"'third part'
anttix
  • 7,709
  • 1
  • 24
  • 25
  • Not sure what you mean about the concatentation? I am trying your way and with or without JSON encoding in PHP DocRaptor is reporting getting an empty string. – jerrygarciuh Jun 27 '12 at 23:11
  • Did you edit the PHP script to take command line use case into account? What if you add echo "$myHTML" as the second command into the script. – anttix Jun 27 '12 at 23:18
  • Error message helped me out! They also accept a document_url argument not shown in examples! Have it working now. Thanks! – jerrygarciuh Jun 27 '12 at 23:18
  • @jerrygarciuh: Please present working code as an answer if it is different than an existing answer. Then select one of the answers (you could even select your own). Thank you for helping making this site better. – hakre Jun 28 '12 at 09:26
0

The examples supplied did not mention a document_url parameter but DocRaptor's error message did.

Working code using what I learned from hakre and anttix!

curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"false", "document_url":"'"http://foo.com/tablemaker.php?CTN=$1"'"}}' http://docraptor.com/docs -o docraptor_sample.pdf
Community
  • 1
  • 1
jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139
  • Now I am confused though. If you remove the line that starts with myHTML=... will the script still work? I think it will and you have saved yourself some memory :) – anttix Jun 28 '12 at 23:15
  • You are correct... Boss took me off of this right after I got it working. He decided to use a screencap service and deliver JPEGs instead of PDFs. I wasn't paying good attention. Will remove from answer. – jerrygarciuh Jun 29 '12 at 16:28