Enterprise Resource Planning Blogs by Members
Gain new perspectives and knowledge about enterprise resource planning in blog posts from community members. Share your own comments and ERP insights today!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Hi all,

I was working on a way to export a report to pdf and then print it out on a printer. Since searching for articles didn't return any results, I figured it out myself and decided to post it myself. The following script makes use of the .net assemblies, so it requires you to have Crystal Reports for Visual Studio installed.

#load the assemblies

[reflection.assembly]::LoadWithPartialName('System.Drawing.Printing')

[reflection.assembly]::LoadWithPartialName('CrystalDecisions.Shared')

[reflection.assembly]::LoadWithPartialName('CrystalDecisions.CrystalReports.Engine')

$ReportLocation = "C:\YourReportPath\report.rpt"

$ReportDestination = "C:\DestinationPath\report.pdf"

$PrinterName = "printer0001"

#setup Crystal Document object

$Report = New-Object CrystalDecisions.CrystalReports.Engine.ReportDocument

#load the Crystal Report

$Report.Load($ReportLocation)


#if your report requires a database connection. Otherwise you can delete this line.

$Report.SetDatabaseLogon('username','password')

#run report and export to pdf

$Report.ExportToDisk($ReportDestination)

#create the required printer and page settings

$PrintOptions = New-Object System.Drawing.Printing.PrinterSettings

$PageOptions = New-Object System.Drawing.Printing.PageSettings

#add the desired printer or it will print to the default printer

$PrintOptions.PrinterName = $PrinterName

$PrintOptions.Copies = 1

#print

$Report.PrintToPrinter($PrintOptions, $PageOptions, $false)

$Report.Dispose()

2 Comments