type:
integration
supports-foxycart-version-from:
0.6.0
system:
pdf
name:
Export to PDF
description:
A simple script to be run locally for converting FoxyCart receipts to printable PDFs.
tags:
accounting, receipts, print
date:
2016-04-29

export to pdf

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 post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

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

  1. Save the code below as transaction_to_pdf.php
  2. In the same folder as this script, create a “receipts” folder.
  3. Make sure your path can run wkhtmltopdf within this folder.
  4. Make sure you have PHP installed and can run it in cli mode.
  5. Set a start date for when you want to begin capturing transactions.
  6. Configure how many months of receipts you want to save.
  7. Configure your store's API key
  8. Configure your store's domain (Example: “yourstore.foxycart.com” or if you're using a custom subdomain, “secure.yourstore.com”)
  9. Run the script locally as: php transaction_to_pdf.php

To view how many receipt files were created per month, you can run this code in bash:

for D in `find . -type d`
do
    echo "${D}":;
    ls "${D}" | wc -l;
done

Requirements

wkhtmltopdf

http://wkhtmltopdf.org/

(have the binary installed in the same folder as this script)

Code

<?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;
}

Site Tools