Hello,
I want to create a scheduled job that exports a crystal report to pdf. I'm using Quartz NuGet as a scheduler. Here's my code:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Quartz;
using System;
using System.IO;
using System.Net;
using System.Web;
namespace TestSchedule.App_code
{
public class LettersJob:IJob
{
public static string text;
public void Execute(IJobExecutionContext context)
{
int appno = 201700002;
int regNum = 1;
PrintLetter(appno, regNum);
}
public void PrintLetter(int appno,int regNum)
{
text = text + "A";
System.IO.File.WriteAllText("C:\\temp\\WriteText.txt", text);
ReportDocument cryRpt = new ReportDocument();
Random random = new Random();
int randomNumber = random.Next(0, 100000);
try
{
if (regNum == 1)
cryRpt.Load(System.Web.HttpContext.Current.Server.MapPath("~/ApproveLetterA.rpt"));
else
{
cryRpt.Load(System.Web.HttpContext.Current.Server.MapPath("~/ApproveLetterB.rpt"));
}
cryRpt.SetParameterValue("@appno", appno);
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = "C:\\temp\\CertNo" + appno + randomNumber + ".pdf";
CrExportOptions = cryRpt.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cryRpt.Export();
}
catch (Exception ex) { Console.Write(ex.Message); }
}
using Quartz;
using Quartz.Impl;
using System;
namespace TestSchedule.App_code
{
public class JobScheduler
{
private IScheduler scheduler;
public void Start()
{
scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<LettersJob>().Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.RepeatForever())
.Build();
scheduler.ScheduleJob(job, trigger);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using TestSchedule.App_code;
namespace TestSchedule
{
public class Global : HttpApplication
{
JobScheduler jobScheduler;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
jobScheduler = new JobScheduler();
jobScheduler.Start();
}
}
}
When I run the PrintLetter on Default.aspx Load I get the exported file as I supposed to.
When I run the PrintLetter through the scheduler I only get the text written in the text file.
Any ideas what am I doing wrong? Is there another way to implement the scheduler?
Thank you in advance.