Created
May 21, 2010 03:54
-
-
Save expectedbehavior/408435 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Doc Raptor simple PHP example | |
* requires pecl_http extension | |
* | |
* This is a simple example of creating an excel file and saving it | |
* using Doc Raptor | |
* | |
* For other usage and examples, visit: | |
* http://docraptor.com/examples | |
* | |
* Doc Raptor http://docraptor.com | |
* Expected Behavior http://www.expectedbehavior.com | |
*/ | |
<?php | |
$api_key = "YOUR_API_KEY_HERE"; | |
$url = "https://docraptor.com/docs?user_credentials=$api_key"; | |
$document_content = "<table><tr><td>Cell</td></tr></table>"; | |
$request = new HTTPRequest($url, HTTP_METH_POST); | |
$request->setPostFields(array('doc[document_content]' => $document_content, | |
'doc[document_type]' => 'xls', | |
'doc[name]' => 'my_doc.xls', | |
'doc[test]' => 'true')); | |
$request->send(); | |
$file = fopen ("my_excel_doc.xls", "w"); | |
fwrite($file, $request->getResponseBody()); | |
fclose ($file); | |
?> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Once you put your API key in below, you can run this using | |
# ruby straight_ruby_example.rb | |
# and two files docraptor_sample.pdf and docraptor_sample.xls will be created in the current directory | |
class Doc | |
API_KEY = "YOUR_API_KEY_HERE" | |
require 'rubygems' | |
require 'httparty' | |
include HTTParty | |
base_uri "https://docraptor.com" | |
# :document_content => content, :name => "a doc", :document_type => "xls", :test => false | |
# returns a string that is the document data | |
def self.create(document_information) | |
post("/docs", :body => {:doc => document_information}, :basic_auth => {:username => API_KEY}) | |
end | |
end | |
File.open("docraptor_sample.xls", "w+") do |f| | |
f.write Doc.create(:document_content => "<table name='Ruby Sheet'><tr><td>Cell 1</td><td>Cell 2</td></tr></table>", | |
:name => "docraptor_sample.xls", | |
:document_type => "xls", | |
:test => true) | |
end | |
File.open("docraptor_sample.pdf", "w+") do |f| | |
f.write Doc.create(:document_content => "<html><body>Text in a PDF</body></html>", | |
:name => "docraptor_sample.pdf", | |
:document_type => "pdf", | |
:test => true) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This example will create a sample excel document with the authentication information as part of the POST data | |
curl -H "Content-Type:application/json" -d'{user_credentials:'YOUR_API_KEY_HERE', doc:{name:"docraptor_sample.xls", document_type:"xls", test:"true", document_content:"<table name=\"My First Sheet\"><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"}}' https://docraptor.com/docs > docraptor_sample.xls | |
#This example will create a sample excel document with the authentication information as part of the URL | |
curl -H "Content-Type:application/json" -d'{doc:{name:"docraptor_sample.xls", document_type:"xls", test:"true", document_content:"<table name=\"My First Sheet\"><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"}}' https://docraptor.com/docs?user_credentials=YOUR_API_KEY_HERE > docraptor_sample.xls | |
#This example will create a sample pdf document with the authentication information as part of the POST data | |
curl -H "Content-Type:application/json" -d'{user_credentials:'YOUR_API_KEY_HERE', doc:{name:"docraptor_sample.pdf", document_type:"pdf", test:"true", document_content:"<html><body>Text in a PDF</body></html>"}}' https://docraptor.com/docs > docraptor_sample.pdf | |
#This example will create a sample pdf document with the authentication information as part of the URL | |
curl -H "Content-Type:application/json" -d'{doc:{name:"docraptor_sample.pdf", document_type:"pdf", test:"true", document_content:"<html><body>Text in a PDF</body></html>"}}' https://docraptor.com/docs?user_credentials=YOUR_API_KEY_HERE > docraptor_sample.pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%table{ :name => "Excel Example" } | |
%tr{ :style => "font-weight:bold;" } | |
%td Document Name | |
%td Document Type | |
%td Test Mode | |
%td Result Url | |
- @save_to_paperclip_examples.each do |example| | |
%tr | |
%td=example.document_name | |
%td=example.document_type | |
%td=example.test | |
%td{ :style => "color:blue;" }=path_to_url(example.document.url) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this function is based on code found: | |
// http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/ | |
// to easily make a form and POST it | |
var download = function(url, data, method){ | |
//url and data options required | |
if( url && data ){ | |
//data can be string of parameters or array/object | |
data = typeof data == 'string' ? data : jQuery.param(data); | |
//split params into form inputs | |
var inputs = ''; | |
jQuery.each(data.split('&'), function(){ | |
var pair = this.split('='); | |
var name = pair.shift(); | |
var value = pair.join("="); | |
inputs+='<textarea name="'+ name +'">'+ value +'</textarea>'; | |
}); | |
//send request | |
jQuery('<form style="display: none" action="' + url | |
+ '" method="' + (method||'post') + '">' | |
+ inputs | |
+ '</form>') | |
.appendTo('body').submit().remove(); | |
}; | |
}; | |
// setup the string represeting the table we want to submit | |
var content = '<table name="foo"><tr><td>word up</td></tr></table>'; | |
// this drops a form on the page and submits, which will result in a download dialog popping up | |
download("http://docraptor.com/docs.xls", | |
"doc[document_content]=" + content | |
+ "&doc[name]=adoc&user_credentials=<YOUR_API_KEY_HERE>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using System.Web; | |
using System.Windows; | |
using Microsoft.Win32; | |
namespace DocRaptorExample | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
private const string PostFormat = "doc[document_content]={0}&doc[name]={1}&doc[document_type]={2}&doc[test]={3}"; | |
private const string ApiKey = "YOUR_API_KEY_HERE"; | |
private static string DocRaptorUrl = String.Format("https://docraptor.com/docs?user_credentials={0}", ApiKey); | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
SampleExcelContentClick(null, null); | |
} | |
private void SampleExcelContentClick(object sender, RoutedEventArgs e) | |
{ | |
_DocumentContent.Text = | |
"<table name='My First Sheet'>\n <tr>\n <td>Cell 1</td>\n <td>Cell 2</td>\n </tr>\n</table>"; | |
} | |
private void SamplePDFContentClick(object sender, RoutedEventArgs e) | |
{ _DocumentContent.Text = "<html>\n <body>\n Some Text in a PDF\n </body>\n</html>"; } | |
private void CreateExcelClick(object sender, RoutedEventArgs e) | |
{ CreateDocument(_DocumentContent.Text, "xls", true); } | |
private void CreatePDFClick(object sender, RoutedEventArgs e) | |
{ CreateDocument(_DocumentContent.Text, "pdf", true); } | |
public static void CreateDocument(string documentContent, string type, bool test) | |
{ | |
var sfd = new SaveFileDialog(); | |
if (sfd.ShowDialog() != true) { return; } | |
var postData = String.Format(PostFormat, HttpUtility.UrlEncode(documentContent), | |
HttpUtility.UrlEncode(sfd.SafeFileName), | |
HttpUtility.UrlEncode(type), | |
HttpUtility.UrlEncode(test.ToString().ToLower())); | |
var byteArray = Encoding.UTF8.GetBytes(postData); | |
var request = (HttpWebRequest)WebRequest.Create(DocRaptorUrl); | |
request.Method = "POST"; | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.ContentLength = byteArray.Length; | |
using (var dataStream = request.GetRequestStream()) | |
{ dataStream.Write(byteArray, 0, byteArray.Length); } | |
try | |
{ | |
var response = request.GetResponse(); | |
using (var dataStream = response.GetResponseStream()) | |
{ | |
using (Stream file = File.OpenWrite(sfd.FileName)) | |
{ CopyStream(dataStream, file); } | |
} | |
MessageBox.Show("File Saved", "Success"); | |
} | |
catch (WebException e) | |
{ | |
if (e.Status == WebExceptionStatus.ProtocolError) | |
{ | |
var response = (HttpWebResponse)e.Response; | |
string errorResponse; | |
using (var dataStream = response.GetResponseStream()) | |
{ | |
using (var reader = new StreamReader(dataStream)) | |
{ errorResponse = reader.ReadToEnd(); } | |
} | |
MessageBox.Show(errorResponse, String.Format("{0} - {1}", response.StatusCode, response.StatusDescription)); | |
} | |
else | |
{ | |
MessageBox.Show(e.Message, "Error"); | |
} | |
} | |
} | |
public static void CopyStream(Stream input, Stream output) | |
{ | |
var buffer = new byte[8 * 1024]; | |
int len; | |
while ((len = input.Read(buffer, 0, buffer.Length)) > 0) | |
{ output.Write(buffer, 0, len); } | |
} | |
private void OpenLinkClick(object sender, RoutedEventArgs e) | |
{ System.Diagnostics.Process.Start(((System.Windows.Documents.Hyperlink)sender).NavigateUri.ToString()); } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this function is based on code found: | |
// http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/ | |
// to easily make a form and POST it | |
var download = function(url, data, method){ | |
//url and data options required | |
if( url && data ){ | |
//data can be string of parameters or array/object | |
data = typeof data == 'string' ? data : $H(data).toQueryString(); | |
//split params into form inputs | |
var inputs = ''; | |
data.split('&').each(function(s){ | |
var pair = s.split('='); | |
var name = pair.shift(); | |
var value = pair.join("="); | |
inputs+='<textarea name="'+ name +'">'+ value +'</textarea>'; | |
}); | |
//send request | |
$$('body')[0].insert('<form id="DocRaptor-example" style="display: none" action="' + url | |
+ '" method="' + (method||'post') + '">' | |
+ inputs + | |
'</form>'); | |
$('DocRaptor-example').submit(); | |
$('DocRaptor-example').remove(); | |
}; | |
}; | |
// setup the string represeting the table we want to submit | |
var content = '<table name="foo"><tr><td>word up</td></tr></table>'; | |
// this drops a form on the page and submits, which will result in a download dialog popping up | |
download("http://docraptor.com/docs.xls", | |
"doc[document_content]=" + content + | |
"&doc[name]=adoc&user_credentials=<YOUR_API_KEY_HERE>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
@save_to_paperclip_examples = SaveToPaperclipExample.all | |
# Don't forget to reigster the xls/pdf mime types in config/initializers/mime_types.rb | |
respond_to do |format| | |
format.html | |
format.xls { send_data Doc.create(:name => "example_xls", :document_content => render_to_string, :document_type => "xls") } | |
format.pdf { send_data Doc.create(:name => "example_pdf", :document_content => render_to_string, :document_type => "pdf") } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment