Exporting Pentaho CDE Dashboards to PDF

Step 1: Configure Sparkl

Sparkl is a plugin creator instrument that sits on 2 major cornerstones of Pentaho: Ctools and PDI, aiming to leverage as much as possible of our existing stack. Dashboards are used for the front-end part and PDI (named Endpoints in sparkl) are used for the back-end logic.

In order to export our dashboards to PDF we only need the endpoint part but we’ll build a small dashboard so we can test the endpoint.

First of all you must install Sparkl using Pentaho Marketplace. Be sure to install dependencies: CDF, CDA, CDE and CGG. It’s recommended to have installed same versions of CTools (14.07.29 STABLE in my case)

Pentaho Marketplace

Restart your BI Server and access Sparkl using the menu Tools as shown in image below.

Sparkl menu

Now you’ll see the initial Sparkl desktop with a default aplication:

Sparkl default desktop

Step 2: Create Sparkl Aplication

Now it’s time to create the aplication! Let’s click on “Add” button and fill the Aplication Description.

Aplication About Description

Select “Elements” tab and click in “Add New Element”. Set a name for the element, select “Kettle Endpoint” as element type and “Clean Job” in template. Now click on “+” button to add it to our application.

Sparkl Kettle Element

You can check your brand new Job in the directory:

"biserver"/pentaho-solutions/system/"app_name"/endpoints/kettle/"element".kjb

Step 3: Edit Kettle Job

Open the Job with Spoon and you’ll see a very simple Job with only “Start” and “Success” steps. Your final Job will look as the following image:

PDF Export Main Job

Our Kettle endpoint will use 3 variables in order to generate PDF file:

  1. URL to render in PDF
  2. Pentaho BIServer directory
  3. Name of PDF file generated

We’re going to call the endpoint using these params so we need to set them in the Job. This is the purpose of the first step, a transformation with variables initialization:

Job variables initialization

Next step is a simple Javascript evaluation to know if our installation is Linux or Windows, because we’re going to use a library call and the script depends on the platform. The code needed to know if the installation is based on linux or windows is:

is_windows

If true then we’re going to use a branch based on Windows scripting and if false then we’ll use a Linux script.

Next step is where “magic” happens. We’re going to use PhantomJS to render our PDF file. The step execute a shell script with following code:

${PENTAHO_FOLDER}\tomcat\webapps\pentaho\phantomjs-lib\phantomjs.exe rasterize.js "${URL_PARAMS}" ${FILE_NAME} A4
(in case of Windows branch)

./phantomjs rasterize.js "${URL_PARAMS}" ${FILE_NAME} A4
(in case of Linux branch)

We must to set Working Directory in the step to

${PENTAHO_FOLDER}\tomcat\webapps\pentaho\phantomjs-lib
(in case of Windows)

${PENTAHO_FOLDER}/tomcat/webapps/pentaho/phantomjs-lib
(in case of Linux)

In last step (PDFFile Transformation) we need to copy our generated PDF file to result.

Job Copy File To Result

In “Get File Name” step we use following selected file:

${PENTAHO_FOLDER}\tomcat\webapps\pentaho\phantomjs-lib\${FILE_NAME}

and then add a step to copy files to result.

Step 4: Finishing Kettle Job

Before we can test the Job we must set a couple of things. First, set Parameters of the Job (double click on empty space in spoon) to force Kettle output to ResultFiles. Remove # in cpk.response.kettleOutput parameter and set the default value to ResultFiles.

The second thing to do is to copy PhantomJS library in Pentaho folder: tomcat/webapps/pentaho/phantomjs-lib.
Download PhantomJS in: http://phantomjs.org/download.html

You need phantomjs.exe, phantomjs and rasterize.js files. checkout files permissions after copy them

Step 4: Testing Kettle Job

Go to your Pentaho Data Integration folder and launch this:

./kitchen.sh -file:/opt/pentaho/biserver-ce/pentaho-solutions/system/pdfExport/endpoints/kettle/pdfJob.kjb -param:urlParamsParameter=http://pentaho.com -param:pentahoParamsParameter=/opt/pentaho/biserver-ce -param:fileParamsParameter=pentaho-landing.pdf

This command will launch our ETL job and it’ll render “pentaho.com” landing page in “pentaho-landing.pdf” file.

If something goes wrong check the log, common errors are typos or file permissions.

Pentaho Landing page in PDF

Step 5: Exporting your Dashboard

I’ve developed a Dashboard for a project (don’t worry about NDA. It’s dummy data) and the client want to export it to PDF.
I have the sparkl app up & running but how can I call it from CDE?? Well, there is several ways to do that, best way is using “datasource” property in Button Component that execute endpoint. You have Success and Failure callbacks properties to add functions to handle function returns.

Pentaho Button Component

Other way is to use Expression property to build the call to our endpoint. All we need to do is invoke endpoint passing params in query:

http://biserver-ip:8080/pentaho/plugin/pdfExport/api/pdfJob&paramurlParamsParameter=http://pentaho.com&paramfileParamsParameter=pentaho-landing.pdf&parampentahoParamsParameter=/opt/pentaho/biserver-ce
same result that previous kitchen call

So we write that function in Expression property in Button Component:

` function f() {

var newURL = “http://” + window.location.host + Dashboards.getWebAppPath(); var url = newURL + ‘/plugin/pdfExport/api/pdfJob?bypassCache=true’; url += ‘&paramurlParamsParameter=’ + encodeURIComponent(urlParamsParameter); url += ‘&paramfileParamsParameter=’ + fileParamsParameter; url += ‘&parampentahoParamsParameter=’ + pentahoParamsParameter;

window.open(url,‘PDF Export’,‘toolbar=0,resizable=1,location=0,status=0’);

}`

‘urlParamsParameter’, ‘fileParamsParameter’ and ‘pentahoParamsParameter’ are Simple Parameters where you can set your values in order to export whatever you want.

Usually you want to set ‘pentahoParamsParameter’ to your biserver-ce installation folder, ‘fileParamsParameter’ to a fixed file name and ‘urlParamsParameter’ to your Dashboard URL. How can you get your Dashboard URL? easy… double click on Tab to open on a new window and copy URL to the parameter.

Opening Dashboard in new Window

Copy Dashboard URL

References:
1. http://www.osbi.fr/exporter-en-pdf-des-dashboards-pentaho-cde-avec-phantomjs/
2. http://diethardsteiner.blogspot.com.es/2014/02/sparkl-create-your-own-app-for-pentaho.html
3. http://fcorti.com/pentaho-sparkl/kettle-endpoint-sparkl-pentaho/

Happy coding!

 

 

 

 

转载: http://joshid.github.io/blog/2014/10/08/pentaho-pdf-export/

在VB.NET中,如果你想要使用Microsoft Print to PDF功能将文档转换成PDF并指定特定的保存路径,你可以通过`System.Printing.PrintQueue`和`System.Drawing.Printing.PrintDocument`类来实现。以下是一个基本的步骤: 1. 首先,你需要添加对`System.Printing`和`System.Drawing.Printing`命名空间的引用。 ```vb.net Imports System.Printing Imports System.Drawing.Printing ``` 2. 创建一个PrintDocument对象,并设置其属性,比如纸张大小、打印范围等。 ```vb.net Dim printDoc As New Printing.PrintDocument() printDoc.DefaultPageSettings = New Printing.PageSettings() printDoc.DefaultPageSettings.Landscape = True ' 如果需要 landscape 方式打印 printDoc.DefaultPageSettings.PaperSize = New Printing.PaperSize("A4") ' 设置纸张类型 ``` 3. 添加一个事件处理器来处理PrintDocument对象的PrintPage事件,这将在每次页面打印前被触发。 ```vb.net Private Sub printDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles printDoc.PrintPage ' 在这里绘制你的内容,例如图形、文本等 End Sub ``` 4. 初始化PrintQueue对象,指向你的打印机,然后创建一个SaveFileDialog让用户选择保存位置。 ```vb.net Dim saveFileDialog As New SaveFileDialog() saveFileDialog.Filter = "PDF files (*.pdf)|*.pdf" If saveFileDialog.ShowDialog() = DialogResult.OK Then Dim filePath As String = saveFileDialog.FileName Dim printerName As String = "YourPrinterName" ' 替换为你实际打印机的名称 Using job As New Printing.PrintJob(printerName, "Exporting to PDF", Nothing) job.Document = printDoc job.Queue高峰时间 = 0 job.Priority = Printing.JobPriority.Normal job.Send() job.WaitForNoMoreEvents(Printing.JobWaitReason.UserRequest) job.Complete(True) ' 完成打印任务 Dim pdfConverter As New PdfSharp.Xps.Pdf.XpsConverterDocument(printDoc) pdfConverter.Save(filePath) End Using End If ``` 5. 当用户点击“保存”按钮时,会弹出文件保存对话框,选择路径后开始将打印任务转换为PDF并保存。 注意:`PdfSharp`库用于将打印文档转换为PDF,如果项目中尚未包含这个库,记得安装它。同时,确保系统的权限允许打印操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值