Print PDF with velocity

Hello
With Servoy 6.03 i have a webclient form with just one big html-aera, dataprovider = vhtml (a form variable).
I fill the variable with:
vhtml = plugins.VelocityReport.renderTemplate(“PasProdukte.html”, m_produktinfos(), resolution);
m_produktinfos collects the needed informations.
The html-aera looks as expected.

Now i only want to print the vhtml into a pdf so the webclient users can do what they want(save,print,open with).

I don’t know how to code this.
Print button in form header with method ???
Any help welcomed.

Regards
Albert

Hi Albert,

If you want to create a PDF via code instead of the viewer you can call the getPDFReport() function which returns the PDF as byte array.
This result you can then save as a binary file using the file plugin, send it via email or store it in a database.

Hope this helps.

Oh, I see you are talking about webclient.
Then as the wiki page says:
“On web client, use previewReport instead with a PREVIEW.MODE set to PDF as this will return a PDF suitable for printing.”

Hope this helps.

Hello
On my form i tried:

var viewer = plugins.VelocityReport.previewReport("PasProdukte.html", m_produktinfos(),null,null,PREVIEW.PDF);

The open/download dialog pops up, but the pdf looks like this:

500
The plugin encountered an internal error and was unable to complete your request...
Exception Message:
Can't load the XML resource (using TRaX transformer).
org.xml.sax.SAXParseException: Content is not allowed in prolog.
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using
TRaX transformer). org.xml.sax.SAXParseException: Content is not allowed in
prolog.
 at
org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:79)
 at
net.stuff.servoy.plugin.velocityreport.PluginProvider.parseContent(PluginProvider.java:
at
net.stuff.servoy.plugin.velocityreport.PluginProvider.doRenderToPDF(PluginProvider.java
at
net.stuff.servoy.plugin.velocityreport.PluginProvider$1.run(PluginProvider.java:722)
Ca
by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException:
Content is not allowed in prolog.
 at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
Source)
 at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
Source)
 at
org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource
... 4 more
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
 at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown
Source)
 at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown
Source)
 ... 7 more

What do i miss?
Regards
Albert

Have a good look at your template.
This error tells you you have something before the <?xml tag (the prolog), which is not valid.

Since you do:
vhtml = plugins.VelocityReport.renderTemplate(“PasProdukte.html”, m_produktinfos(), resolution);
before, have a look at the vhtml content, this will show you what’s before the prolog, you need to get rid of that (even if it is a simple blank line!)

And BTW, why the “null, null” in your parameters? And why trying to get the viewer, in web client this method doesn’t return it, it is always null, so:
plugins.VelocityReport.previewReport(“PasProdukte.html”, m_produktinfos(), PREVIEW.PDF);
should work.

Hello

I cleaned my html

<html>
<head>


<style type="text/css">
  body {
    color: black; background-color: white;
    font-size: 12px;
    font-family: Courier,Fixedsys,Helvetica,Arial,sans-serif;
   
  }

 img.bild {
    margin: 0 0 0.7em; padding: 0em;
    height: 150px;
    width: 150px;
    border: 2px double;
  }

/*  
	table {
		-fs-table-paginate: paginate;
	}
*/

</style>
</head>

<body width="100%">

<h1><B>  Produkt Informationen </B>  </h1>


$bild.getImageTag({'class': "bild"})





<div style="position:absolute; top:95px; left:200px; width:400px;
background-color:#ffffff; border:1px solid #804000; padding:10px">
<B> 
Name__________: $produktname

Produktgruppe_: $produktgruppe

Produkttype___: $produkttyp

Hersteller____: $produkthersteller

#if ($phbis > 0)
PH-Von________: $phvon 

PH-Bis________: $phbis 

KZ-Pflicht____: $kzpflicht

GGSVSE:_______: $ggsvse
 
#end
</B>
</div>


<h2>Beschreibung:</h2>
$htmlize.get($beschreibung)



<h2>Details:</h2>
$htmlize.get($details)




<h2>Anwendung:</h2>

$htmlize.get($anwendung)







<h2>Artikel:</h2>
#tablizer($dataseta, "Artikel", true)





<a href="javascript:m_dateiDownload()">Hello</a>

<h2>Dokumente:</h2>
  <table>
  <thead>
    <tr>
     <th>Datei</th>
     <th>Typ</th>
    </tr>
  </thead>
  <tbody>
  #foreach($record in $datasetd)
    <tr>
     <td><a href="javascript:forms.Komplettanzeige_Produkt.m_dateiDownload($record.PRODUKTDOKUMENT)">$record.PRODUKTDOKUMENT</a></td>
     <td>$record.DOKUMENTTYP</td>
    </tr>
  #end
  </tbody>
  </table>



</body>
</html>

and i changed my call to:

var viewer = plugins.VelocityReport.previewReport("PasProdukte.html", m_produktinfos(),PREVIEW.PDF);

Same result?

Regards
Albert

When you say “same result” do you mean you get the same exception exactly?
Still “org.xml.sax.SAXParseException: Content is not allowed in prolog.”?

Because I see one thing that the renderer will not like as well: usage of UPPERCASE tags is not allowed especially if you mix them up with lowercase as well).

So you need to use tags instead of
And make sure that all your tags are properly closed.

Hello

I changed B to b

The PDF still contains:

500
The plugin encountered an internal error and was unable to complete your request...
Exception Message:
Can't load the XML resource (using TRaX transformer).
org.xml.sax.SAXParseException: Content is not allowed in prolog.
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using
TRaX transformer). org.xml.sax.SAXParseException: Content is not allowed in
prolog.
 at
org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:79)
 at
net.stuff.servoy.plugin.velocityreport.PluginProvider.parseContent(PluginProvider.java:
at
net.stuff.servoy.plugin.velocityreport.PluginProvider.doRenderToPDF(PluginProvider.java
at
net.stuff.servoy.plugin.velocityreport.PluginProvider$1.run(PluginProvider.java:722)
Ca
by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException:
Content is not allowed in prolog.
 at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
Source)
 at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
Source)
 at
org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource
... 4 more
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
 at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown
Source)
 at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown
Source)
 ... 7 more

???
Regards
Albert

Is your template UTF-8 (or 16)?
Can you make sure that you don’t have a UTF-BOM as a first character?
If your document has one, this will also give you this error…

Some text editors will offer you the option to remove BOM from UTF files, to know more about BOM, have a look at:
http://docs.moodle.org/21/en/UTF-8_and_ … the_BOM.3F

I’ve committed a change to the VelocityReport plugin - v1.5.8 - to deal with Unicode BOM. The templates are read and if they contain BOM bytes, these are stripped from the input using a spcial Reader.
This should fix your issue without even changing your file encoding: https://www.servoyforge.net/news/190

Hello

Working with v1.5.8 !!
Thank you very much.
Regards
Albert