Skip to content

Instantly share code, notes, and snippets.

@f1code
Created January 26, 2016 15:00
Show Gist options
  • Select an option

  • Save f1code/b5678732e6303f35c015 to your computer and use it in GitHub Desktop.

Select an option

Save f1code/b5678732e6303f35c015 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using SSSWorld.Common;
using System.IO;
using System.Data;
namespace SSSWorld.RFI.NotificationGenerator
{
class CreatePdfFromCrystal
{
public void printCoverPage(string ticketID, int numPages, string slxConnStr, string outputFileName)
{
string reportName = "Reports\\CoverPage.rpt";
using (CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new ReportDocument())
{
doc.FileName = reportName;
doc.SetParameterValue("TicketId", ticketID);
doc.SetParameterValue("NumPagesAttached", numPages);
SetLogonInfo(slxConnStr, doc);
doc.ExportToDisk(ExportFormatType.PortableDocFormat, outputFileName);
doc.Close();
}
}
public void printOverallCoverPage(string contactID, DateTime startDate, DateTime endDate, bool split, string slxConnStr, string outputFileName)
{
String reportName = "Reports\\OverallCoverPage.rpt";
using (CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new ReportDocument())
{
doc.FileName = reportName;
doc.SetParameterValue("CONTACTID", contactID);
doc.SetParameterValue("STARTDATE", startDate.Date);
doc.SetParameterValue("ENDDATE", endDate.Date.AddDays(1));
doc.SetParameterValue("SPLIT", split);
SetLogonInfo(slxConnStr, doc);
doc.ExportToDisk(ExportFormatType.PortableDocFormat, outputFileName);
doc.Close();
}
}
public void printCustomerApproval(String ticketNumber, IDBConnectionWrapper db, String outputFileName)
{
string reportName = GetCustomCAReport(ticketNumber, db);
using (CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new ReportDocument())
{
doc.FileName = reportName;
doc.RecordSelectionFormula = "{TICKET.TICKETID} = \"" + ticketNumber + "\"";
SetLogonInfo(db.ConnectionString, doc);
doc.ExportToDisk(ExportFormatType.PortableDocFormat, outputFileName);
doc.Close();
}
}
public void printReport(string reportOutput, string reportName, string ticketId, IDBConnectionWrapper db)
{
String rptFile = ExtractReport(reportName, db);
using (CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new ReportDocument())
{
doc.FileName = rptFile;
doc.RecordSelectionFormula = "{TICKET.TICKETID} = \"" + ticketId + "\"";
SetLogonInfo(db.ConnectionString, doc);
doc.ExportToDisk(ExportFormatType.PortableDocFormat, reportOutput);
doc.Close();
}
}
private string GetCustomCAReport(string ticketNumber, IDBConnectionWrapper db)
{
String reportName = (String)db.GetField("F.REPORT_NAME", @"C_FORM_DOC F
JOIN C_ACC_FORM AF ON AF.C_FORM_DOCID=F.C_FORM_DOCID
JOIN TICKET T ON T.STORE_ACCOUNTID=AF.ACCOUNTID",
"AF.BUNDLE='T' AND F.FORMTYPE='CA' AND T.TICKETID=?", ticketNumber);
if (reportName == null)
// default report
return "Reports\\CustomerApproval.rpt";
return ExtractReport("Form Docs:" + reportName, db);
}
public void printInstallerCommitment(String ticketId, String slxConnStr, String outputFileName)
{
string reportName = "Reports\\InstallerCommitment.rpt";
createOutput(ticketId, slxConnStr, reportName, outputFileName);
}
public void createOutput(String ticketId, String slxConnStr, String reportFileName, String outputFileName)
{
using (CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new ReportDocument())
{
doc.FileName = reportFileName;
doc.SetParameterValue("TicketId", ticketId);
SetLogonInfo(slxConnStr, doc);
doc.ExportToDisk(ExportFormatType.PortableDocFormat, outputFileName);
doc.Close();
}
}
private void SetLogonInfo(string connStr, ReportDocument reportDoc)
{
ConnectionInfo conInfo = GetConnectionInformation(connStr);
SetDBLogonForReport(conInfo, reportDoc);
SetDBLogonForSubreports(conInfo, reportDoc);
}
/// <summary>
/// Extract database info from the connection string
/// </summary>
private ConnectionInfo GetConnectionInformation(string connStr)
{
ConnectionInfo conInfo = new ConnectionInfo();
conInfo.DatabaseName = GetParameterFromConnection(connStr, "Initial Catalog");
conInfo.UserID = GetParameterFromConnection(connStr, "User ID");
conInfo.Password = GetParameterFromConnection(connStr, "Password");
conInfo.ServerName = GetParameterFromConnection(connStr, "Data Source");
return conInfo;
}
/// <summary>
/// Extract a parameter from the connection string.
/// </summary>
/// <param name="paramName"></param>
/// <returns></returns>
private String GetParameterFromConnection(String connectionString, String paramName)
{
Regex re = new Regex(paramName + "\\s*=\\s*\"?([^\";]+)", RegexOptions.IgnoreCase);
Match m = re.Match(connectionString);
if (m.Success)
return m.Groups[1].Value;
return "";
}
private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
private void SetDBLogonForSubreports(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo, subReportDocument);
}
}
}
}
/// <summary>
/// Extract report from database to local file.
/// Return local path.
/// </summary>
/// <param name="reportName"></param>
/// <returns></returns>
private String ExtractReport(String reportName, IDBConnectionWrapper db)
{
int i;
String[] familyName = reportName.Split(new char[] { ':' }, 2);
if (familyName.Length != 2)
throw new InvalidOperationException("Invalid report name");
if (familyName[0].IndexOfAny(new char[] { '\\', '/', ':' }) >= 0 ||
familyName[1].IndexOfAny(new char[] { '\\', '/', ':' }) >= 0)
throw new InvalidOperationException("Invalid report name");
String destination = Path.Combine("Reports", String.Format("{0}.rpt", familyName[1]));
using (IDataReader reader = db.OpenDataReader("select modifydate, data from plugin where type=19 and family=? and name=?",
familyName[0], familyName[1]))
{
if (!reader.Read())
throw new InvalidOperationException("Report " + reportName + " not found.");
DateTime modifyDate = reader.GetDateTime(0);
if (File.Exists(destination) && File.GetLastWriteTime(destination) >= modifyDate)
return destination;
byte[] buffer = (byte[])reader[1];
for (i = 0; i < buffer.Length; i++)
{
if (buffer[i] == 0xd0 && buffer[i + 1] == 0xcf && buffer[i + 2] == 0x11)
break;
if (i >= buffer.Length)
{
// For troubleshooting..
//using (FileStream output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None))
//{
// output.Write(buffer, i, buffer.Length);
//}
throw new Exception("Invalid plugin data (could not locate report data)");
}
}
using (FileStream output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None))
{
output.Write(buffer, i, buffer.Length - i);
}
}
return destination;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment