The following script shows how you can call the FOP server from a .NET program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml;
// input xml data
xml="<?xml version=\"1.0\" encoding=\"iso-8859-1\"?> <departments><department><departmentName>R&D</departmentName> <person> <name>John Schmidt</name>
";
xml=xml +"<address>Red street 3</address> <status>A</status> </person> <person> <name>Paul Bones</name> <address>White street 5</address> <status>A</status>
";
xml=xml +"</person> <person> <name>Mark Mayer</name> <address>Blue street 5</address> <status>A</status> </person> <person> <name>Janet Black</name>
";
xml=xml +"<address>Black street 8</address> <status>I</status> </person></department><department> <departmentName>Sales</departmentName> <person>
";
xml=xml +"<name>Juan Gomez</name> <address>Green street 3</address> <status>A</status> </person> <person> <name>Juliet Bones</name> <address>White street 5</address>
";
xml = xml + " <status>A</status> </person></department></departments>";
// call URL of FOP server passing the template name (XSL-FO file)
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8087/J4LFOPServer/servlet?TEMPLATE=departmentEmployees.fo");
webRequest.ContentType = "text/xml";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(xml);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send XML bytes
}
catch (WebException ex)
{
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{ // get the response
HttpWebResponse webResponse =
(HttpWebResponse)webRequest.GetResponse();
Stream pdfStream=webResponse.GetResponseStream();
long len = webResponse.ContentLength;
byte[] pdfBytes=new
byte[len];
pdfStream.Read(pdfBytes,0,(int) len);
pdfStream.Close();
// save PDF to file
FileStream fileOs=new
FileStream("output.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write);
fileOs.Write(pdfBytes, 0, pdfBytes.Length);
fileOs.Close();
}
catch (WebException ex)
{
}
}
}
}
|