First thing we will do is generate the required Java source for the Document package, like so:
wsimport http://localhost/CFIDE/services/document.cfc?wsdl -d bin/ -s
src/ -p com.coldfusion.document
This will create the required Java code to access the Document ColdFusion Web Service.
One of the easiest ways of creating a brand new PDF Document is by converting an HTML document into a PDF via ColdFusion's Document Web Service.
If you browse to http://localhost/CFIDE/services/document.cfc, you will get a basic view of the methods that are available on this Web Service, and also the arguments that you are able to take advantage of when calling this Web Service.
Except for "serviceusername" and "servicepassword", the Document Web Service's arguments corresponds directly to a ColdFusion Tag called "cfdocument", for which the documentation can be found http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859">here
You can see there are a variety of options for generating a PDF from HTML, which give us a lot of control over what gets generated in our PDF.
Let us attempt to generate a simple "Hello World!" PDF file. First we will write some HTML:
<a href="String html = "<html>"" +
"<head>" +
"<title>Hello World!</title>" +
"<head>" +
"<body>" +
"<p>Hello World!</p>" +
"</body>" +
"</html>";">String html = "<html>" +
"<head>" +
"<title>Hello World!</title>" +
"<head>" +
"<body>" +
"<p>Hello World!</p>" +
"</body>" +
"</html>"</a>
We create a new DocumentService, and request a Document object from it:
DocumentService service = new DocumentService();
Document document = service.getDocumentCfc();
There are a wide variety of parameters that can be passed into the "document.generate()" method, but there are only a few that we are concerned with at the moment:
- serviceUsername - this is the username for our ColdFusion as a Service User.
- servicePasswor d - this is the passwor d for our ColdFusion as a Service User.
- content - the HTML content we ar e passing through.
Pass "null" into the arguments of the ColdFusion Web Service methods in which you wish to use the defaults values.
String result = document.generate("cfaas", "cfaas", null, html, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null);
System.out.println(result);
This will return a String, for example: http://localhost/CFFileServlet/_cfservicelayer/_cf3733600242229376232.pdf
Which is a URL to a temporary download of the PDF you have generated. If you browse to that URL, it should open a PDF that says "Hello World!".
To get more complicated with the PDF generation, we can add a header to our document.
To do this we need to cr eate a List of "DocumentItem", with some values set on the objects.
You can find the documentation for the "DocumentItem" object here: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/ WSc3ff6d0ea77859461172e0811cbec22c24-7758.html
For example:
Documentitem item = new Documentitem();
item.setType("header");
item.setContent("My Document Header");
List <Documentitem> documentItems = new ArrayList <Documentitem>();
documentItems.add(item);
String result = document.generate("cfaas", "cfaas", null, html, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null, documentItems);
System.out.println(result);
This creates a header and puts the content of "My Document Header", and passes it through to "generate" method. If you browse to the URL that is r eturned, a PDF with the header specified should be displayed.
There are many other things we can use to control how we author our PDF as well, but this should give you some of the basics to get you started.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}