Examples

C# example

Below is represented a simple C# program which demonstrate how to use the service from .NET environment. At the first stage, the program creates a HTTP POST request embedding the source color.fo file into the request. Then it submits the request to http://api.xep.com/format address and waits to a response. After receiving the response, it parses the response XML, extracting the document link and the formatting log. At the last stage, it downloads the document.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace XepApiDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Encoding encoding = Encoding.UTF8;
            string boundary = "--AaB03x";

            Stream formDataStream = new MemoryStream();

            byte[] foData = null;
            using (FileStream fs = new FileStream(@"c:\Temp\color.fo", 
                                                  FileMode.Open, FileAccess.Read))
            {
                foData = new byte[fs.Length];
                fs.Read(foData, 0, foData.Length);
            }

            string header = string.Format("--{0}\r\nContent-Disposition: form-data; " +
                                          "name=\"{1}\"; filename=\"{2}\";\r\n " +
                                          "Content-Type: application/octet-stream\r\n\r\n", 
                                          boundary, "xml", @"c:\Temp\color.fo");
            formDataStream.Write(encoding.GetBytes(header), 0, header.Length);         
            formDataStream.Write(foData, 0, foData.Length);
            string footer = "\r\n--" + boundary + "--\r\n";
            formDataStream.Write(encoding.GetBytes(footer), 0, footer.Length);

            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();

            HttpWebRequest request = WebRequest.Create("http://api.xep.com/format/") 
                                          as HttpWebRequest;

            request.Method = "POST";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.UserAgent = "API";
            request.CookieContainer = new CookieContainer();
            request.ContentLength = formData.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(formData, 0, formData.Length);
                requestStream.Close();
            }

            HttpWebResponse webResponse = request.GetResponse() as HttpWebResponse;

            StreamReader respReader = new StreamReader(webResponse.GetResponseStream());
            string fullResponse = respReader.ReadToEnd();
            webResponse.Close();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(fullResponse);
            String link = doc.GetElementsByTagName("document")[0].InnerText;
            String log = doc.GetElementsByTagName("log")[0].InnerText;
            System.Console.Write(log);

            WebClient webclient = new WebClient();
            webclient.DownloadFile(link, @"c:\Temp\color.pdf");
        }
    }
}

Python example

Below is represented a simple Python script which demonstrate how to call the service from Python programming language. The script submits hammer.xml XML document, its hammer.xsl XSLT stylesheet and nail.jpg picture to the service and downloads the formatted PDF document. Before launching the script you should install the poster library. You can download it from here or simply launch "pip install poster" command.

import sys
import urllib2
import webbrowser
from xml.dom.minidom import parseString

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

register_openers()
datagen, headers = multipart_encode({"xml": open("hammer.xml", "rb"),
                                     "xsl": open("hammer.xsl", "rb"),
                                     "image1": open("nail.jpg", "rb")})
request = urllib2.Request("http://api.xep.com/format/", datagen, headers)
response = urllib2.urlopen(request).read()

doc = parseString(response)
link = None
try:
    log =  doc.getElementsByTagName("log")[0].childNodes[0].data
    link =  doc.getElementsByTagName("document")[0].childNodes[0].data
except:
    pass

if link:
    webbrowser.open(link, new=1)
Copyright © RenderX inc.        Design by DjangoSlingshot.com