Differences

This shows you the differences between two versions of the page.

Link to this comparison view

integration:export_to_pdf [2016/04/29 17:36]
foxyluke [Requirements]
integration:export_to_pdf [2017/04/26 07:02]
Line 1: Line 1:
----- dataentry integration ---- 
-type          : integration ​    # do not change this line 
-supports-foxycart-version-from : 0.6.0    # Minimum required FoxyCart version for this to work 
-supports-foxycart-version-to ​  : ​    # Last FoxyCart version that supports this (leave empty if unknown) 
-systems ​      : pdf                # the system(s) that the integration is for, separated with commas if more than one 
-name          : Export to PDF                # the name of the integration 
-description ​  : ​ A simple script to be run locally for converting FoxyCart receipts to printable PDFs.                
-tags_tags ​    : accounting, receipts, print                # tags, separated by commas. don't include the "​system"​ here. 
-date_dt ​      : 2016-04-29 ​               # the date in YYYY-MM-DD format 
-version ​      : ​                # if you have a version for your code, enter it here 
-developer_url :                 # if you'd like a link back to your site, stick it here 
----- 
  
- 
- 
-====== export to pdf ====== 
- 
-<WRAP important round> 
-//**Please note:**// 
-The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please [[http://​forum.foxycart.com/​|post in our forum]], but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance. 
-</​WRAP>​ 
- 
-===== Description ===== 
- 
-This script uses the API to get all your receipt urls and store them in PDFs, one folder per month. 
- 
-Author: Luke Stokes 
- 
-Use however you want and take personal responsibility when doing so. 
- 
-===== Installation ===== 
- 
-  - Save the code below as transaction_to_pdf.php 
-  - In the same folder as this script, create a "​receipts"​ folder. 
-  - Make sure your path can run wkhtmltopdf within this folder. 
-  - Make sure you have PHP installed and can run it in cli mode. 
-  - Set a start date for when you want to begin capturing transactions. 
-  - Configure how many months of receipts you want to save. 
-  - Configure your store'​s API key 
-  - Configure your store'​s domain (Example: "​yourstore.foxycart.com"​ or if you're using a custom subdomain, "​secure.yourstore.com"​) 
-  - Run the script locally as: php transaction_to_pdf.php 
- 
-===== Requirements ===== 
- 
-wkhtmltopdf 
- 
-http://​wkhtmltopdf.org/​ 
- 
-(have the binary installed in the same folder as this script) 
- 
-===== Code ===== 
-<code php> 
-<?php 
-/** 
-* Transactions to PDF 
-* A simple script to be run locally for converting FoxyCart receipts to printable PDFs. 
-* This script uses the API to get all your receipt urls and store them in PDFs, one folder per month. 
-* 
-* Author: Luke Stokes 
-* Use however you want and take personal responsibility when doing so. 
-* 
-* Requirements:​ wkhtmltopdf 
-* http://​wkhtmltopdf.org/​ 
-* (have the binary installed in the same folder as this script) 
-* 
-* Instructions:​ 
-* 1) In the same folder as this script, create a "​receipts"​ folder. 
-* 2) Make sure your path can run wkhtmltopdf within this folder. 
-* 3) Make sure you have PHP installed and can run it in cli mode. 
-* 4) Set a start date for when you want to begin capturing transactions. 
-* 5) Configure how many months of receipts you want to save. 
-* 6) Configure your store'​s API key 
-* 7) Configure your store'​s domain (Example: "​yourstore.foxycart.com"​ or if you're using a custom subdomain, "​secure.yourstore.com"​) 
-* 8) Run via: 
-        php transaction_to_pdf.php 
-*/ 
- 
-// Set your timezone 
-date_default_timezone_set('​America/​Chicago'​);​ 
- 
-// Configure this start date 
-$start_date = new DateTime('​2015-01-01'​);​ 
- 
-// Configure how many months of transactions you want to save 
-$months_to_save = 12; 
- 
-// Put your API Token here 
-$api_token = '​XXXXXXXXXXXXX';​ 
- 
-// Put your store domain here 
-$domain = '​yourstore.foxycart.com';​ 
- 
- 
-// ----- End Configuration ----- 
- 
-for ($i = 0; $i < $months_to_save;​ $i++) { 
-    $start_date_string = date_format($start_date,​ '​Y-m-d'​);​ 
-    $start_date->​add(new DateInterval('​P1M'​));​ 
-    $end_date_string = date_format($start_date,​ '​Y-m-d'​);​ 
-    saveReceiptsAsPDF( 
-        $domain, 
-        $api_token, 
-        $start_date_string,​ 
-        $end_date_string 
-        ); 
-} 
- 
-function saveReceiptsAsPDF($domain,​ $api_token, $start_date,​ $end_date) 
-{ 
-    $folder_name = $start_date . '​_to_'​ . $end_date; 
-    $foxyData = array(); 
-    $foxyData["​api_token"​] = $api_token; 
-    $foxyData["​api_action"​] = "​transaction_list";​ 
-    $foxyData["​transaction_date_filter_begin"​] = $start_date;​ 
-    $foxyData["​transaction_date_filter_end"​] = $end_date; 
-    $transactions = getTransactions($domain,​ $foxyData); 
-    $output_folder = '​./​receipts/'​ . $folder_name;​ 
-    print "​\nMaking folder $output_folder\n";​ 
-    @mkdir($output_folder);​ 
-    foreach ($transactions as $transaction) { 
-        print "​Saving $transaction->​id...\n";​ 
-        exec('​wkhtmltopdf ' . $transaction->​receipt_url . ' ' . $output_folder . '/'​ . $transaction->​id . '​.pdf'​);​ 
-    } 
-} 
- 
-function getTransactions($domain,​ $foxyData, $additional_results = array()) 
-{ 
-    $result = $additional_results;​ 
-    $ch = curl_init();​ 
-    curl_setopt($ch,​ CURLOPT_URL,​ "​https://"​ . $domain . "/​api"​);​ 
-    curl_setopt($ch,​ CURLOPT_POSTFIELDS,​ $foxyData); 
-    curl_setopt($ch,​ CURLOPT_RETURNTRANSFER,​ 1); 
-    curl_setopt($ch,​ CURLOPT_CONNECTTIMEOUT,​ 5); 
-    curl_setopt($ch,​ CURLOPT_TIMEOUT,​ 15); 
-    $response = trim(curl_exec($ch));​ 
-    curl_close($ch);​ 
-    $resp = new SimpleXMLElement($response);​ 
-    if ($resp->​result == '​SUCCESS'​) { 
-        foreach($resp->​transactions->​transaction as $transaction) { 
-            $result[] = $transaction;​ 
-        } 
-    } 
-    // keep going if we have more data to work with 
-    if ((int)$resp->​statistics->​filtered_total > (int)$resp->​statistics->​pagination_end) { 
-        $foxyData['​pagination_start'​] = (int)$resp->​statistics->​pagination_end+1;​ 
-        $result = getTransactions($domain,​$foxyData,​$result);​ 
-    } 
-    return $result; 
-} 
-</​code>​ 

Site Tools