[Solved]Change BaseReports pulled by Button from .odt to PDF

Getting your data onto paper - or the web - Discussing the reports features of Base

[Solved]Change BaseReports pulled by Button from .odt to PDF

Postby Andy-Jordaan » Fri Aug 21, 2015 3:36 am

On the Forum there are many examples of Code to pull Base Reports from a Button, two good examples are :
https://forum.openoffice.org/en/forum/viewtopic.php?f=13&t=76213&p=347235&hilit=base+report+pdf#p347235 see "F3K Total" comment
and
https://forum.openoffice.org/en/forum/viewtopic.php?f=42&t=77961&p=356774&hilit=.loadComponentFromURL#p356774 see "Nocton" comment

Both are painless to install , operate well but deliver a writer document, for security reasons I need the document delivered as PDF

In the Forum are many examples on converting a document, (Calc and Writer) to PDF, I can find none that do this from a base Report button, I have tried cobbling many examples to solve this issue but all fail on the final "uno:ExportDirectToPDF" or other PDF conversion string, it seems to me, with my limited skills, to be failing on the document name passing through to the pdf code
here is a code that, with a fixed name pulls a report
https://forum.openoffice.org/en/forum/viewtopic.php?f=13&t=78386&p=359067&hilit=reports+PDF+macro+PDF+Reports#p359067
here is Zenlord's coding trove.
https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=56713&p=249604#p249604

But here is my attempt
Code: Select all   Expand viewCollapse view
Option Explicit

Sub Reports_PDF(oEvent as object)
Rem  Romkes code 
REM https://forum.openoffice.org/en/forum/viewtopic.php?f=39&t=45263&p=209235&hilit=ReportDocuments#p209235
    DIM oDoc AS Object, oSettings AS Object
    dim oCreateButton,oForm ' This are form details
    dim oConnection,oReportsDocuments
    oCreateButton=oEvent.source.model
    if oCreateButton.tag = "" then
       msgbox "No report name in the button tag" & chr(13) & "This is the third from below of the property on the common tabpage"
    else ' Now we have to start to find all kind of objects
       oForm=oCreateButton.parent
          oConnection=oForm.Activeconnection
          oReportsDocuments=oConnection.parent.DatabaseDocument.ReportDocuments ' This point to the report containers
          if oReportsDocuments.hasByHierarchicalName(oCreateButton.tag) then
             'Call openreport with the parameters
             openReport(oConnection, oReportsDocuments, oCreateButton.tag , oForm.Filter)
          else print "There is a wrong report name"
          end if
    end if
End Sub



Function openReport(oConnection, oReportsDocuments,aReportName,sFilter) as object

       dim dispatcher as object   
       dim aProp(2) as new com.sun.star.beans.PropertyValue
       dim aProp2 ' Make a variant
       dim oReportDesign
       aProp(1).Name = "ActiveConnection"
       aProp(1).Value = oConnection
       aProp(0).Name = "Hidden"
       aProp(0).Value = true   
       aProp(2).Name = "OpenMode"
       aProp(2).Value = "openDesign"
       let aProp2=aProp
       oReportDesign=oReportsDocuments.loadComponentFromURL(aReportName ,"",0,aProp())
       aProp2(0).Value = false
       aProp2(2).Value = "open"
       
REM   ***** Pulls ".ODT" report fine from "openReport" and from "oDoc"   *******
       openReport=oReportsDocuments.loadComponentFromURL(aReportName ,"",23,aProp2())
'       oDoc=oReportsDocuments.loadComponentFromURL(aReportName ,"",23,aProp2())
REM
oDoc = ThisComponent.createInstance("com.sun.star.document.Settings")

   dispatcher=createUnoService("com.sun.star.frame.DispatchHelper")

        Dim a(2) As New com.sun.star.beans.PropertyValue
a(0).Name = "URL"        : a(0).Value = "my_file_name_.pdf"
a(1).Name = "FilterName" : a(1).Value = "writer_pdf_Export"       

REM ***********************
REM Error Here " Object variable not set. "
REM ***********************       
   dispatcher.executeDispatch(oDoc, ".uno:ExportDirectToPDF", "", 0, args())
   
End Function



Can anybody give any advice on where I am going Wrong ? :crazy:
Last edited by RoryOF on Fri Aug 21, 2015 12:30 pm, edited 1 time in total.
Reason: Added [Solved] and green tick [RoryOF, Moderator]
Ubuntu 16.04
Libre office 5.1
Andy-Jordaan
 
Posts: 26
Joined: Wed Jul 15, 2015 9:51 am
Location: South Africa

Re: Change Base Reports pulled by Button from .odt to PDF

Postby F3K Total » Fri Aug 21, 2015 9:37 am

Hi,
try this code, insert the name of the report into the tag-field of the button:
Code: Select all   Expand viewCollapse view
Sub Store_report_as_pdf(event)
    oButton = event.source.Model
    sReportname = oButton.tag
    sTimestamp = format (date(),"YYYYMMDD") + "_" + format (time(),"HHMM")
    surlfolderDB = replace(thisdatabasedocument.URL,thisdatabasedocument.title,"")
    surlfolder = surlfolderDB+"Output/"
    if not FileExists (surlfolder) then mkdir surlfolder
    sFilename = ""+sReportname+"_"+sTimestamp+"_"+".pdf"
    surl = surlfolder+sFilename
    if FileExists (surl) then
      if msgbox (""""+sFilename+""" is already existing, replace?",49,"pdf Export") = 2 then exit sub
    endif
    oReportDocuments = thisdatabasedocument.reportdocuments
    oreport = oReportDocuments.getbyname(sReportname).open
    dim pdfProperties(1) as new com.sun.star.beans.PropertyValue
    dim odtProperties(0) as new com.sun.star.beans.PropertyValue
    pdfProperties(0).Name = "FilterName"
    pdfProperties(0).Value = "writer_pdf_Export"
    oreport.storeToURL(surl, pdfProperties())
    if msgbox ("Folder: "+convertfromurl(surlfolder)+chr(13)+"Filename: "+sFilename+chr(13)+chr(13)+"Close report?",292,"Success, report saved as .pdf") = 7 then exit sub
    oreport.close -1
end sub

R
    MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
    my current system
  • Windows 7 OOo, AOO, LOLinux Mint OOo, AOO, LO
F3K Total
Volunteer
 
Posts: 923
Joined: Fri Dec 16, 2011 8:20 pm

[SOLVED] Change Base Reports pulled by Button from .odt to P

Postby Andy-Jordaan » Fri Aug 21, 2015 12:08 pm

OK seems to be sorted out, not by skill but a comedy of errors

This code works on embedded Base folder but not in the "My Macro's " folder

Code: Select all   Expand viewCollapse view
REM Empty   PDF_URL = "File:///home/andy/Desktop/Authorisation.pdf"
REM
REM
Sub Reports(oEvent as object)
    DIM oDoc AS Object, oSettings AS Object
    dim oCreateButton,oForm ' This are form details
    dim oConnection,oReportsDocuments
    oCreateButton=oEvent.source.model
    if oCreateButton.tag = "" then
       msgbox "No report name in the button tag" & chr(13) & "This is the third from below of the property on the common tabpage"
    else ' Now we have to start to find all kind of objects
       oForm=oCreateButton.parent
          oConnection=oForm.Activeconnection
          oReportsDocuments=oConnection.parent.DatabaseDocument.ReportDocuments ' This point to the report containers
          if oReportsDocuments.hasByHierarchicalName(oCreateButton.tag) then
             'Call openreport with the parameters
             openReport(oConnection, oReportsDocuments, oCreateButton.tag , oForm.Filter)
          else print "There is a wrong report name"
          end if
    end if
End Sub

Function openReport(oConnection, oReportsDocuments,aReportName,sFilter) as object
       dim aProp(2) as new com.sun.star.beans.PropertyValue
       dim aProp2 ' Make a variant
       dim oReportDesign
       aProp(1).Name = "ActiveConnection"
       aProp(1).Value = oConnection
       aProp(0).Name = "Hidden"
       aProp(0).Value = true
       aProp(2).Name = "OpenMode"
       aProp(2).Value = "openDesign"
       let aProp2=aProp
       oReportDesign=oReportsDocuments.loadComponentFromURL(aReportName ,"",0,aProp())
        aProp2(0).Value = false
       aProp2(2).Value = "open"
'       openReport=oReportsDocuments.loadComponentFromURL(aReportName ,"",23,aProp2())
'      oDoc=oReportsDocuments.loadComponentFromURL(aReportName ,"",23,aProp2())
oReport = ThisDatabaseDocument.ReportDocuments.getByName(aReportName).open

Dim myProps(0) as New com.sun.star.beans.PropertyValue
myProps(0).Name="FilterName"
myProps(0).Value = "writer_pdf_Export"
surl = converttourl("file:///home/andy/PDF/"+ aReportName + ".pdf")
oReport.storetoUrl(surl,myProps())

End Function

Ubuntu 16.04
Libre office 5.1
Andy-Jordaan
 
Posts: 26
Joined: Wed Jul 15, 2015 9:51 am
Location: South Africa

[SOLVED] Change Base Reports pulled by Button from .odt to P

Postby Andy-Jordaan » Fri Aug 21, 2015 12:27 pm

Thank you, Thank you, Thank you

This is Great Code "F3K Total"

It give me exactly what I need and even generates its own folder and still gives a Time-Stamp, that alone is a few week work for me.

Most Appreciated
Ubuntu 16.04
Libre office 5.1
Andy-Jordaan
 
Posts: 26
Joined: Wed Jul 15, 2015 9:51 am
Location: South Africa


Return to Reporting

Who is online

Users browsing this forum: No registered users and 1 guest