The purpose of this document is to illustrate how we can print an SSRS report in PDF format to disk or file-system in X++.Ability to print an SSRS report in PDF format to disk or file-system automatically.
SSRS report is successfully deployed to the report server.
Code:
Please find below the job to write SSRS report in PDF to disk or file-system.
static void makPrintSSRSReportToDisk(Args _args) { //Instantiate custom controller SRSPrintDestinationSettings printSettings; SrsReportRunController controller = new PurchPurchaseOrderController(); controller.parmReportName(ssrsReportStr(PurchPurchaseOrder, Report)); //Get print settings printSettings = controller.parmReportContract().parmPrintSettings(); // print medium printSettings.printMediumType(SRSPrintMediumType::File); printSettings.fileFormat(SRSReportFileFormat::PDF); printSettings.overwriteFile(true); printSettings.fileName(@"C:\Temp\PurchPurchaseOrder.pdf"); // parameter dialog controller.parmShowDialog(false); controller.startOperation(); }
OR TRY below code
Step 1: add new query with CustinvoiceJour Table
Step 2: below class which opens PDF file path to save Report as PDF format and File Name Format
class SalesInvoicePDFPrintToDesk extends RunBaseBatch
{
#File
QueryRun queryRun;
FilePath filePath;
str 100 fileNameFormat;
DialogField dlfFilePath;
DialogField dlfFileNameFormat;
#DEFINE.CurrentVersion(2)
#LOCALMACRO.CurrentList
filePath,
fileNameFormat
#ENDMACRO
}
Object dialog()
{
DialogRunBase dialogRunBase = super();
dialogRunBase.addGroup("@SYS80053");
dlfFilePath = dialogRunBase.addFieldValue(extendedTypeStr(FilePath), filePath);
dlfFileNameFormat = dialogRunBase.addFieldValue(extendedTypeStr(Description255), fileNameFormat, "@SYS40708");
dlfFileNameFormat.widthMode(1);
dialogRunBase.addText("Format: \n[I] - Invoice number\n[C] - Customer account\n[N] - Customer name\n[P] - Parent account name\n[D] - Invoice date\n[R] - Running number", 500);
return dialogRunBase;
}
public container findReportNameAndType(CustInvoiceJour _custInvoiceJour)
{
PrintMgmtSettings printMgmtSettings;
PrintMgmtDocInstance printMgmtDocInstance;
PrintMgmtReportFormat reportFormat;
PrintMgmtDocumentType documentType;
CustTable custTable = _custInvoiceJour.custTable_InvoiceAccount();
;
select firstonly printMgmtDocInstance
order by priorityId
where printMgmtDocInstance.ReferencedTableId == tableNum(CustTable)
&& printMgmtDocInstance.ReferencedRecId == custTable.RecId
&& printMgmtDocInstance.DocumentType == PrintMgmtDocumentType::SalesOrderInvoice
&& printMgmtDocInstance.NodeType == PrintMgmtNodeType::CustTable
join printMgmtSettings
where printMgmtSettings.ParentId == printMgmtDocInstance.RecId;
reportFormat = PrintMgmtReportFormat::find(printMgmtSettings.ReportFormat);
// If no report format found, use the system report format
if(!reportFormat)
{
documentType = (select printMgmtDocInstance where printMgmtDocInstance.RecId == printMgmtSettings.ParentId).DocumentType;
reportFormat = PrintMgmtReportFormat::findSystem(documentType);
}
return [reportFormat.Name, reportFormat.MorphX];
}
private FileName getFileName(CustInvoiceJour _custInvoiceJour, int _number)
{
FileName fileName;
FileName tmpCustPart;
int ix;
fileName = filePath;
for (ix = 1; ix <= strLen(fileNameFormat); ix++)
{
if (substr(fileNameFormat, ix, 1) == '[')
{
switch (substr(fileNameFormat, ix + 1, 1))
{
case 'I':
fileName += _custInvoiceJour.InvoiceId;
break;
case 'C':
fileName += _custInvoiceJour.OrderAccount;
break;
case 'N':
tmpCustPart = _custInvoiceJour.custTable_OrderAccount().name();
tmpCustPart = strrem(tmpCustPart, '/');
tmpCustPart = strrem(tmpCustPart, ':');
tmpCustPart = strrem(tmpCustPart, ',');
fileName += tmpCustPart;
break;
case 'P':
tmpCustPart = _custInvoiceJour.custTable_InvoiceAccount().name();
tmpCustPart = strrem(tmpCustPart, '/');
tmpCustPart = strrem(tmpCustPart, ':');
tmpCustPart = strrem(tmpCustPart, ',');
fileName += tmpCustPart;
break;
case 'D':
fileName += date2str(_custInvoiceJour.InvoiceDate, 123, 2, 2, 2, 2, 2);
break;
case 'R':
fileName += int2str(_number);
break;
}
ix += 2;
}
else
{
fileName += substr(fileNameFormat, ix, 1);
}
}
fileName += #pdf;
return fileName;
}
public boolean getFromDialog()
{
boolean ret = super();
filePath = dlfFilePath.value();
fileNameFormat = dlfFileNameFormat.value();
return ret;
}
public void initParmDefault()
{
super();
filePath = '';
fileNameFormat = '';
this.queryBuild();
}
container pack()
{
return [#CurrentVersion, #CurrentList, queryRun.pack()];
}
protected void queryBuild()
{
queryRun = new QueryRun(queryStr(SalesInvoicePDFPrintCustomReport));
}
public QueryRun queryRun()
{
return queryRun;
}
void run()
{
CustInvoiceJour custInvoiceJour;
Args args;
SysOperationProgress progressBar;
int ix;
int total;
SalesInvoiceController salesInvoiceController;
SalesInvoiceContract salesInvoiceContract;
MenuFunction menuFunction;
MenuItemNameOutPut menuItemName;
ReportName reportName;
ReportRun reportRun;
PrintJobSettings printJobSettings;
SrsReportRunImpl srsReportRun;
SrsPrintMgmtExecutionInfo executionInfo;
#AviFiles
if (substr(filePath, strLen(filePath), 1) != "\\")
{
filePath += "\\";
}
total = SysQuery::countTotal(queryRun);
queryRun.reset();
progressBar = SysOperationProgress::newGeneral(#AviUpdate, "@SUP1033", total); // Invoice PDF Export
while (queryRun.next())
{
if (queryRun.changed(tableNum(custInvoiceJour)))
{
custInvoiceJour = queryRun.get(tableNum(CustInvoiceJour));
ix++;
// Do not show progressbar when in batch
if (!this.isInBatch())
{
progressBar.incCount();
progressBar.update(true);
progressBar.setText(strfmt("@SUP2870", ix, total));
}
// [menuItemName, morphXReportType] = this.findReportNameAndType(custInvoiceJour);
if( morphXReportType == NoYes::Yes)
{
menuFunction = New MenuFunction(menuItemName, MenuItemType::Output);
reportName = menuFunction.object();
reportName = 'SalesInvoiceCustomFromatReport';
if (!reportName)
continue;
args = new Args();
args.caller(this);
args.name(reportName);
args.parm("PDFPrint");
args.parmEnumType(PrintCopyOriginal);
args.parmEnum(PrintCopyOriginal::Original);
args.record(custInvoiceJour);
reportRun = new ReportRun(args);
// Get settings from report
printJobSettings = reportRun.printJobSettings();
printJobSettings = reportRun.printJobSettings();
printJobSettings.fitToPage(true);
//printJobSettings.virtualPageHeight(0);
// force PDF printing
printJobSettings.format(PrintFormat::PDF);
printJobSettings.setTarget(PrintMedium::File);
printJobSettings.viewerType(ReportOutputUserType::PDF);
printJobSettings.fileName(this.getFileName(custInvoiceJour, ix));
printJobSettings.doNotOverwrite(false);
printJobSettings.lockDestinationProperties(true);
// Save values
reportRun.unpackPrintJobSettings(printJobSettings.packPrintJobSettings());
// Run the report (create the PDF file)
reportRun.run();
}
else
{
reportName = menuItemName; //SalesInvoice.Report
if (!reportName)
{
reportName = 'SalesInvoice.Report';
}
args = new args();
args.record(custInvoiceJour);
salesInvoiceController = new SalesInvoiceController();
salesInvoiceController.parmReportName(reportName);
salesInvoiceContract = salesInvoiceController.parmReportContract().parmRdpContract();
salesInvoiceContract.parmRecordId(custInvoiceJour.RecId); // RecId must be passed otherwise the report will be empty
salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo()); // comment this code if tested in pre release
salesInvoiceController.parmArgs(args);
srsReportRun = salesInvoiceController.parmReportRun() as SrsReportRunImpl;
salesInvoiceController.parmReportRun(srsReportRun);
salesInvoiceController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);
salesInvoiceController.parmReportContract().parmPrintSettings().overwriteFile(true);
salesInvoiceController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);
salesInvoiceController.parmReportContract().parmPrintSettings().fileName(this.getFileName(custInvoiceJour, ix));
executionInfo = new SrsPrintMgmtExecutionInfo();
salesInvoiceController.parmReportContract().parmReportExecutionInfo(executionInfo);
try
{
salesInvoiceController.runReport();
}
catch
{
////retry;
}
}
}
}
}
Comments
Post a Comment