Migliorata gestioen stampe, testato anche task.run ma da errori server
This commit is contained in:
@@ -5,3 +5,9 @@ dotnet_diagnostic.CA1031.severity = none
|
||||
|
||||
# CA1303: Non passare valori letterali come parametri localizzati
|
||||
dotnet_diagnostic.CA1303.severity = none
|
||||
|
||||
# CA1305: Specificare IFormatProvider
|
||||
dotnet_diagnostic.CA1305.severity = none
|
||||
|
||||
# CA1806: Non ignorare i risultati del metodo
|
||||
dotnet_diagnostic.CA1806.severity = none
|
||||
|
||||
+2
-4
@@ -5,10 +5,8 @@
|
||||
</startup>
|
||||
<appSettings>
|
||||
<add key="appName" value="LPA" />
|
||||
<!-- conf server comunicazione x NKC Ufficio -->
|
||||
<!--<add key="serverBaseAddr" value="http://iis02/NKC/" />-->
|
||||
<!-- conf server comunicazione x dev MP-MAG -->
|
||||
<add key="serverBaseAddr" value="http://iis01/NKC/" />
|
||||
<add key="serverBaseAddr" value="http://iis02/NKC/" />
|
||||
<add key="serverIp" value="iis02" />
|
||||
<add key="localReportPath" value="reports" />
|
||||
<add key="localPdfPath" value="pdf" />
|
||||
<add key="_logDir" value="/logs/" />
|
||||
|
||||
+160
-51
@@ -23,6 +23,10 @@ namespace LPA
|
||||
/// </summary>
|
||||
protected List<string> pqList;
|
||||
/// <summary>
|
||||
/// Indirizzo base del server x test ping
|
||||
/// </summary>
|
||||
protected string serverIp;
|
||||
/// <summary>
|
||||
/// Indirizzo base del server cui connettersi
|
||||
/// </summary>
|
||||
protected string serverBaseAddr;
|
||||
@@ -145,7 +149,20 @@ namespace LPA
|
||||
private void setupConf()
|
||||
{
|
||||
baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
serverBaseAddr = memLayer.ML.CRS("serverBaseAddr");
|
||||
// cerco se ho conf da json
|
||||
if (!string.IsNullOrEmpty(srvSetupJson.serverIp))
|
||||
{
|
||||
serverBaseAddr = srvSetupJson.serverBaseAddr;
|
||||
serverIp = srvSetupJson.serverIp;
|
||||
}
|
||||
// altrimenti da app.config
|
||||
else
|
||||
{
|
||||
serverBaseAddr = memLayer.ML.CRS("serverBaseAddr");
|
||||
serverIp = memLayer.ML.CRS("serverIp");
|
||||
// salvo...
|
||||
srvSetupJson = new serverSetup() { serverBaseAddr = serverBaseAddr, serverIp = serverIp };
|
||||
}
|
||||
localReportPath = $"{AppDomain.CurrentDomain.BaseDirectory}{memLayer.ML.CRS("localReportPath")}\\";
|
||||
vetoPrint = DateTime.Now;
|
||||
}
|
||||
@@ -419,7 +436,41 @@ namespace LPA
|
||||
set
|
||||
{
|
||||
string fullPath = $"{baseDir}conf\\{qSetFilePath}";
|
||||
string rawData = JsonConvert.SerializeObject(value);
|
||||
string rawData = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
File.WriteAllText(fullPath, rawData);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Configurazione server da json file
|
||||
/// </summary>
|
||||
protected serverSetup srvSetupJson
|
||||
{
|
||||
get
|
||||
{
|
||||
serverSetup answ = new serverSetup();
|
||||
string fullPath = $"{baseDir}conf\\{srvSetFilePath}";
|
||||
// cerco file x leggere conf...
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
string rawData = File.ReadAllText(fullPath);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
answ = JsonConvert.DeserializeObject<serverSetup>(rawData);
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
utils.lgError("Error in deserialization of srvSetupJson file", exc);
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
set
|
||||
{
|
||||
string fullPath = $"{baseDir}conf\\{srvSetFilePath}";
|
||||
string rawData = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
File.WriteAllText(fullPath, rawData);
|
||||
}
|
||||
}
|
||||
@@ -880,7 +931,7 @@ namespace LPA
|
||||
int numTot = 0;
|
||||
string urlApiQueue = $"{serverBaseAddr}/api/PrintQueue";
|
||||
string urlApiQueueLen = $"{serverBaseAddr}/api/PrintQueueLen";
|
||||
// verifico il modo di controllo code: SE è signle --> chiamo metodo con le SOLE code gestite...
|
||||
// verifico il modo di controllo code: SE è single --> chiamo metodo con le SOLE code gestite...
|
||||
if (!string.IsNullOrEmpty(memLayer.ML.CRS("queueCheckMode")))
|
||||
{
|
||||
// se ho code configurate
|
||||
@@ -899,59 +950,96 @@ namespace LPA
|
||||
urlApiQueueLen = $"{serverBaseAddr}/api/PrintQueueLen?queueList={listManQueue}";
|
||||
}
|
||||
}
|
||||
string rawData = callUrl(urlApiQueueLen);
|
||||
// verifico non ci sia veto print..
|
||||
DateTime adesso = DateTime.Now;
|
||||
if (vetoPrint < adesso)
|
||||
{
|
||||
vetoPrint = adesso.AddSeconds(15);
|
||||
// verifico ping!
|
||||
if (!utils.pingAddress(serverIp))
|
||||
{
|
||||
// registro errore ping... attendo 3 sec
|
||||
utils.lgError("Error: ping not responding");
|
||||
vetoPrint = adesso.AddSeconds(3);
|
||||
}
|
||||
else
|
||||
{
|
||||
// verifico LEN coda
|
||||
string rawData = callUrl(urlApiQueueLen);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
try
|
||||
{
|
||||
int.TryParse(rawData, out numTot);
|
||||
lblQueueCount.Text = numTot.ToString("000");
|
||||
if (numTot > 0)
|
||||
{
|
||||
// disabilitato perché da errore di concorrenza...
|
||||
bool useTask = false;
|
||||
if (useTask)
|
||||
{
|
||||
List<Task> tasks = new List<Task>();
|
||||
// se ho stampe in coda --> verifico code gestite
|
||||
foreach (var coda in queueSetupConf)
|
||||
{
|
||||
Task t = Task.Run(() => processQueue(urlApiQueue, coda));
|
||||
tasks.Add(t);
|
||||
}
|
||||
Task.WaitAll(tasks.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
// se ho stampe in coda --> verifico code gestite
|
||||
foreach (var coda in queueSetupConf)
|
||||
{
|
||||
processQueue(urlApiQueue, coda);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
utils.lgError("Error in deserialization during refreshQueueStatus", exc);
|
||||
}
|
||||
}
|
||||
vetoPrint = adesso;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Processa singola coda
|
||||
/// </summary>
|
||||
/// <param name="urlApiQueue"></param>
|
||||
/// <param name="coda"></param>
|
||||
private void processQueue(string urlApiQueue, KeyValuePair<string, printQueue> coda)
|
||||
{
|
||||
string rawData;
|
||||
string urlSingleQueue = $"{urlApiQueue}/{coda.Key}";
|
||||
rawData = callUrl(urlSingleQueue);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
try
|
||||
{
|
||||
// deserializzo
|
||||
int.TryParse(rawData, out numTot);
|
||||
lblQueueCount.Text = numTot.ToString("000");
|
||||
if (numTot > 0)
|
||||
List<reportData> report2print = JsonConvert.DeserializeObject<List<reportData>>(rawData);
|
||||
if (report2print != null)
|
||||
{
|
||||
// se ho stampe in coda --> verifico code gestite
|
||||
foreach (var coda in queueSetupConf)
|
||||
// aggiorno contatore...
|
||||
coda.Value.NumWaiting = report2print.Count;
|
||||
// verifico sia RUNNING...
|
||||
if (coda.Value.isRunning)
|
||||
{
|
||||
string urlSingleQueue = $"{urlApiQueue}/{coda.Key}";
|
||||
rawData = callUrl(urlSingleQueue);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
// deserializzo
|
||||
List<reportData> report2print = JsonConvert.DeserializeObject<List<reportData>>(rawData);
|
||||
if (report2print != null)
|
||||
{
|
||||
// aggiorno contatore...
|
||||
coda.Value.NumWaiting = report2print.Count;
|
||||
// verifico sia RUNNING...
|
||||
if (coda.Value.isRunning)
|
||||
{
|
||||
// verifico non ci sia veto print..
|
||||
DateTime adesso = DateTime.Now;
|
||||
if (vetoPrint < adesso)
|
||||
{
|
||||
vetoPrint = adesso.AddSeconds(20);
|
||||
printSingleQueue(urlApiQueue, coda, report2print);
|
||||
vetoPrint = adesso;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printSingleQueue(urlApiQueue, coda, report2print);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
utils.lgError("Error in deserialization during refreshQueueStatus", exc);
|
||||
utils.lgError($"Error in deserialization during processQueue | urlSingleQueue {urlSingleQueue} | rawData {rawData}", exc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Effettua la stampa di una singola coda...
|
||||
/// </summary>
|
||||
@@ -960,20 +1048,41 @@ namespace LPA
|
||||
/// <param name="report2print"></param>
|
||||
private void printSingleQueue(string urlApiQueue, KeyValuePair<string, printQueue> coda, List<reportData> report2print)
|
||||
{
|
||||
// metodi thread da rivedere... ma si "Pestano i piedi" in scarico
|
||||
////Task.Run(() => Method1()).Wait();
|
||||
////t.Wait();
|
||||
///
|
||||
//List<Task> tasks = new List<Task>();
|
||||
//Task t = Task.Run(() => printSingleQueue(urlApiQueue, coda, report2print));
|
||||
//tasks.Add(t);
|
||||
//Task.WaitAll(tasks.ToArray());
|
||||
|
||||
#if false
|
||||
//bool doParallel = false;
|
||||
//if (doParallel)
|
||||
//{
|
||||
// Parallel.ForEach(report2print, item =>
|
||||
// {
|
||||
// bool fatto = false;
|
||||
// // se ho almeno 1 stampa --> lancio!
|
||||
// queueConf confCoda = listQueue.Find(conf => conf.name == coda.Key);
|
||||
// if (confCoda != null)
|
||||
// {
|
||||
// fatto = rPrint.printReport(confCoda.template, localReportPath, coda.Value.Printer, item.rdsData, confCoda.deviceInfoParam, true);
|
||||
// }
|
||||
// // fatta stampa aggiorno server con indicazione che ho stampato
|
||||
// if (fatto)
|
||||
// {
|
||||
// coda.Value.NumWaiting--;
|
||||
// coda.Value.NumDone++;
|
||||
// // compongo payload...
|
||||
// printTask printResult = new printTask()
|
||||
// {
|
||||
// ticketNum = item.ticketNum,
|
||||
// newStatus = 1,
|
||||
// message = ""
|
||||
// };
|
||||
// string payload = JsonConvert.SerializeObject(printResult);
|
||||
// // chiamo in put print result!
|
||||
// postUrl(urlApiQueue, payload);
|
||||
// }
|
||||
// });
|
||||
//}
|
||||
#endif
|
||||
|
||||
// oggetto x stampa report...
|
||||
reportPrinter rPrint = new reportPrinter();
|
||||
// array dei task di stampa...
|
||||
List<Task> tasks = new List<Task>();
|
||||
foreach (var item in report2print)
|
||||
{
|
||||
bool fatto = false;
|
||||
|
||||
@@ -21,4 +21,16 @@ namespace LPA
|
||||
/// </summary>
|
||||
public int counter { get; set; } = 0;
|
||||
}
|
||||
|
||||
public class serverSetup
|
||||
{
|
||||
/// <summary>
|
||||
/// Indirizzo (ip/DNS) del server
|
||||
/// </summary>
|
||||
public string serverIp { get; set; } = "";
|
||||
/// <summary>
|
||||
/// Path URL
|
||||
/// </summary>
|
||||
public string serverBaseAddr { get; set; } = "";
|
||||
}
|
||||
}
|
||||
|
||||
+262
-262
@@ -14,274 +14,274 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace LPA
|
||||
{
|
||||
public class reportPrinter
|
||||
{
|
||||
/// <summary>
|
||||
/// Directory di abse applicazione
|
||||
/// </summary>
|
||||
protected string baseDir;
|
||||
/// <summary>
|
||||
/// Path x salvataggio locale pdf
|
||||
/// </summary>
|
||||
protected string localPdfPath = "pdf";
|
||||
/// <summary>
|
||||
/// Classe reportprinter
|
||||
/// </summary>
|
||||
public reportPrinter()
|
||||
public class reportPrinter
|
||||
{
|
||||
baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
localPdfPath = memLayer.ML.confReadString("localPdfPath");
|
||||
}
|
||||
|
||||
#region metodi stampa effettiva report
|
||||
|
||||
/// <summary>
|
||||
/// Indice pagina corrente x stampa ogni report
|
||||
/// </summary>
|
||||
private int m_currentPageIndex;
|
||||
/// <summary>
|
||||
/// stream del report...
|
||||
/// </summary>
|
||||
private IList<Stream> m_streams;
|
||||
/// <summary>
|
||||
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante + eventuale backup pdf
|
||||
/// </summary>
|
||||
/// <param name="reportTemplate">Nome report (file)</param>
|
||||
/// <param name="localReportPath">Cartella locale dei report</param>
|
||||
/// <param name="printerName">nome completo stampante (rispetto al server)</param>
|
||||
/// <param name="rdsData">tabella dati da caricare nel report</param>
|
||||
/// <param name="deviceInfoParam">parametri "device input"</param>
|
||||
/// <param name="doPdfCopy">effettua ANCHE copia pdf</param>
|
||||
public bool printReport(string reportTemplate, string localReportPath, string printerName, Dictionary<string, DataTable> rdsData, devInfoParam deviceInfoParam, bool doPdfCopy)
|
||||
{
|
||||
bool answ = false;
|
||||
if (deviceInfoParam != null)
|
||||
{
|
||||
using (LocalReport report = new LocalReport())
|
||||
/// <summary>
|
||||
/// Directory di abse applicazione
|
||||
/// </summary>
|
||||
protected string baseDir;
|
||||
/// <summary>
|
||||
/// Path x salvataggio locale pdf
|
||||
/// </summary>
|
||||
protected string localPdfPath = "pdf";
|
||||
/// <summary>
|
||||
/// Classe reportprinter
|
||||
/// </summary>
|
||||
public reportPrinter()
|
||||
{
|
||||
//LocalReport report = new LocalReport();
|
||||
report.EnableExternalImages = true;
|
||||
report.ReportPath = $"{localReportPath}{reportTemplate}";
|
||||
// per ogni oggetto rds nome/tabela aggiungo la sorgente dati...
|
||||
string rdsName = "";
|
||||
DataTable tabDati = null;
|
||||
foreach (var item in rdsData)
|
||||
{
|
||||
rdsName = item.Key;
|
||||
tabDati = item.Value;
|
||||
report.DataSources.Add(new ReportDataSource(rdsName, tabDati));
|
||||
}
|
||||
|
||||
// stampa da EMF
|
||||
answ = doEmfPrint(printerName, report, deviceInfoParam.xmlParam);
|
||||
// controllo se devo fare copia PDF... stampiamosu pdf su una folder locale
|
||||
if (doPdfCopy)
|
||||
{
|
||||
devInfoParam devInfoPdf = new devInfoParam("PDF", deviceInfoParam.PageHeight, deviceInfoParam.PageWidth, deviceInfoParam.MarginLeft, deviceInfoParam.MarginRight, deviceInfoParam.MarginTop, deviceInfoParam.MarginBottom);
|
||||
// salva ANCHE pdf
|
||||
doLocalPdfPrint(report, devInfoPdf.xmlParam);
|
||||
}
|
||||
baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
localPdfPath = memLayer.ML.confReadString("localPdfPath");
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// esegue print vero e proprio
|
||||
/// </summary>
|
||||
/// <param name="printerName"></param>
|
||||
/// <param name="report"></param>
|
||||
/// <param name="deviceInfo"></param>
|
||||
private bool doEmfPrint(string printerName, LocalReport report, string deviceInfo)
|
||||
{
|
||||
bool fatto = false;
|
||||
// export in EMF
|
||||
Export(report, deviceInfo);
|
||||
//ExportPDF(report, deviceInfo);
|
||||
m_currentPageIndex = 0;
|
||||
// stampo
|
||||
fatto = Print(printerName);
|
||||
// do dispose?
|
||||
Dispose();
|
||||
return fatto;
|
||||
}
|
||||
/// <summary>
|
||||
/// effettua stampa in PDF dei vari report in una cartella Anno/Mese/Giorno
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
/// <param name="deviceInfo"></param>
|
||||
private void doLocalPdfPrint(LocalReport report, string deviceInfo)
|
||||
{
|
||||
// export in PDF
|
||||
ExportPDF(report, deviceInfo);
|
||||
m_currentPageIndex = 0;
|
||||
// do dispose?
|
||||
Dispose();
|
||||
}
|
||||
/// <summary>
|
||||
/// Export del report come EMF (Enhanced Metafile) file.
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
private void Export(LocalReport report, string deviceInfo)
|
||||
{
|
||||
Warning[] warnings;
|
||||
m_streams = new List<Stream>();
|
||||
report.Render("Image", deviceInfo, CreateStream, out warnings);
|
||||
foreach (Stream stream in m_streams)
|
||||
{
|
||||
//stream.Flush();
|
||||
stream.Position = 0;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Export del report come PDF file.
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
private void ExportPDF(LocalReport report, string deviceInfo)
|
||||
{
|
||||
Warning[] warnings;
|
||||
m_streams = new List<Stream>();
|
||||
report.Render("PDF", deviceInfo, CreateStreamPdf, out warnings);
|
||||
foreach (Stream stream in m_streams)
|
||||
{
|
||||
//stream.Flush();
|
||||
stream.Position = 0;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// dispone l'applicazione e rilascia le risorse
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_streams != null)
|
||||
{
|
||||
foreach (Stream stream in m_streams)
|
||||
{
|
||||
stream.Close();
|
||||
}
|
||||
m_streams = null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// ciclo da fornire al renderizzatore dei report, per salvare 1 immagine da ogni pagina del report
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="fileNameExtension"></param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <param name="willSeek"></param>
|
||||
/// <returns></returns>
|
||||
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
||||
{
|
||||
// compongo stringhe x file con nomi univoci
|
||||
DateTime adesso = DateTime.Now;
|
||||
string filePathName = $"{baseDir}{localPdfPath}\\temp\\{name}_{adesso:HHmmss}_{adesso:ffffff}.{fileNameExtension}";
|
||||
Stream stream = new FileStream(filePathName, FileMode.Create);
|
||||
m_streams.Add(stream);
|
||||
return stream;
|
||||
}
|
||||
/// <summary>
|
||||
/// ciclo da fornire al renderizzatore dei report, per salvare 1 pdf da ogni pagina del report
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="fileNameExtension"></param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <param name="willSeek"></param>
|
||||
/// <returns></returns>
|
||||
private Stream CreateStreamPdf(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
||||
{
|
||||
// compongo stringhe
|
||||
DateTime adesso = DateTime.Now;
|
||||
string dailyDir = $"{baseDir}{localPdfPath}\\{adesso:yyyy}\\{adesso:MM}\\{adesso:dd}\\";
|
||||
string pdfPathName = $"{dailyDir}\\{name}_{adesso:HHmmss}_{adesso:ffff}.{fileNameExtension}";
|
||||
// creo Directory se non c'è
|
||||
fileMover fm = new fileMover(dailyDir, "");
|
||||
fm.checkDir();
|
||||
Stream stream = new FileStream(pdfPathName, FileMode.Create);
|
||||
m_streams.Add(stream);
|
||||
return stream;
|
||||
}
|
||||
/// <summary>
|
||||
/// Handler per PrintPageEvents
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="ev"></param>
|
||||
private void PrintPage(object sender, PrintPageEventArgs ev)
|
||||
{
|
||||
try
|
||||
{
|
||||
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
|
||||
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
|
||||
m_currentPageIndex++;
|
||||
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Instance.Error($"Errore in PrintPage{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// funzione di stampa...
|
||||
/// </summary>
|
||||
private bool Print(string printerName)
|
||||
{
|
||||
if (m_streams == null || m_streams.Count == 0)
|
||||
return false;
|
||||
|
||||
bool fatto = false;
|
||||
// provo a stampare
|
||||
try
|
||||
{
|
||||
using (PrintDocument printDoc = new PrintDocument())
|
||||
#region metodi stampa effettiva report
|
||||
|
||||
/// <summary>
|
||||
/// Indice pagina corrente x stampa ogni report
|
||||
/// </summary>
|
||||
private int m_currentPageIndex;
|
||||
/// <summary>
|
||||
/// stream del report...
|
||||
/// </summary>
|
||||
private IList<Stream> m_streams;
|
||||
/// <summary>
|
||||
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante + eventuale backup pdf
|
||||
/// </summary>
|
||||
/// <param name="reportTemplate">Nome report (file)</param>
|
||||
/// <param name="localReportPath">Cartella locale dei report</param>
|
||||
/// <param name="printerName">nome completo stampante (rispetto al server)</param>
|
||||
/// <param name="rdsData">tabella dati da caricare nel report</param>
|
||||
/// <param name="deviceInfoParam">parametri "device input"</param>
|
||||
/// <param name="doPdfCopy">effettua ANCHE copia pdf</param>
|
||||
public bool printReport(string reportTemplate, string localReportPath, string printerName, Dictionary<string, DataTable> rdsData, devInfoParam deviceInfoParam, bool doPdfCopy)
|
||||
{
|
||||
printDoc.PrinterSettings.PrinterName = printerName;
|
||||
if (!printDoc.PrinterSettings.IsValid)
|
||||
{
|
||||
Log.Instance.Error($"Impostazioni non valide per la stampante {printerName}");
|
||||
bool answ = false;
|
||||
if (deviceInfoParam != null)
|
||||
{
|
||||
using (LocalReport report = new LocalReport())
|
||||
{
|
||||
//LocalReport report = new LocalReport();
|
||||
report.EnableExternalImages = true;
|
||||
report.ReportPath = $"{localReportPath}{reportTemplate}";
|
||||
// per ogni oggetto rds nome/tabela aggiungo la sorgente dati...
|
||||
string rdsName = "";
|
||||
DataTable tabDati = null;
|
||||
foreach (var item in rdsData)
|
||||
{
|
||||
rdsName = item.Key;
|
||||
tabDati = item.Value;
|
||||
report.DataSources.Add(new ReportDataSource(rdsName, tabDati));
|
||||
}
|
||||
|
||||
// stampa da EMF
|
||||
answ = doEmfPrint(printerName, report, deviceInfoParam.xmlParam);
|
||||
// controllo se devo fare copia PDF... stampiamosu pdf su una folder locale
|
||||
if (doPdfCopy)
|
||||
{
|
||||
devInfoParam devInfoPdf = new devInfoParam("PDF", deviceInfoParam.PageHeight, deviceInfoParam.PageWidth, deviceInfoParam.MarginLeft, deviceInfoParam.MarginRight, deviceInfoParam.MarginTop, deviceInfoParam.MarginBottom);
|
||||
// salva ANCHE pdf
|
||||
doLocalPdfPrint(report, devInfoPdf.xmlParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// esegue print vero e proprio
|
||||
/// </summary>
|
||||
/// <param name="printerName"></param>
|
||||
/// <param name="report"></param>
|
||||
/// <param name="deviceInfo"></param>
|
||||
private bool doEmfPrint(string printerName, LocalReport report, string deviceInfo)
|
||||
{
|
||||
bool fatto = false;
|
||||
// export in EMF
|
||||
Export(report, deviceInfo);
|
||||
m_currentPageIndex = 0;
|
||||
// stampo
|
||||
fatto = Print(printerName);
|
||||
// do dispose...
|
||||
Dispose();
|
||||
return fatto;
|
||||
}
|
||||
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
||||
printDoc.Print();
|
||||
fatto = true;
|
||||
};
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Instance.Error($"Errore in Print{Environment.NewLine}{exc}");
|
||||
}
|
||||
return fatto;
|
||||
}
|
||||
/// <summary>
|
||||
/// effettua pulizia della cartella temp x i files più vecchi di X ore (web.config)
|
||||
/// </summary>
|
||||
public void pulisciDir()
|
||||
{
|
||||
// num max ore di età x files "vecchi" da tenere in temp area...
|
||||
int maxAgeMinutes = memLayer.ML.CRI("maxAgeTempArea");
|
||||
int eliminati = 0;
|
||||
// ottengo elenco files *.emf
|
||||
fileMover.obj.setDirectory($"{baseDir}{localPdfPath}\\temp\\");
|
||||
FileInfo[] _fis = fileMover.obj.elencoFiles_FI("*.emf");
|
||||
bool fatto = false;
|
||||
foreach (FileInfo _file in _fis)
|
||||
{
|
||||
if (_file.CreationTime < DateTime.Now.AddMinutes(-maxAgeMinutes)) // elimino files vecchi...
|
||||
{
|
||||
fatto = fileMover.obj.eliminaFile(_file);
|
||||
if (fatto)
|
||||
{
|
||||
Log.Instance.Info($"Eliminato file {_file.Name}");
|
||||
eliminati++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// salvo il log degli update
|
||||
if (eliminati > 0)
|
||||
{
|
||||
Log.Instance.Info($"Eliminati {eliminati} files temporanei da area temp");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// effettua stampa in PDF dei vari report in una cartella Anno/Mese/Giorno
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
/// <param name="deviceInfo"></param>
|
||||
private void doLocalPdfPrint(LocalReport report, string deviceInfo)
|
||||
{
|
||||
// export in PDF
|
||||
ExportPDF(report, deviceInfo);
|
||||
m_currentPageIndex = 0;
|
||||
// do dispose?
|
||||
Dispose();
|
||||
}
|
||||
/// <summary>
|
||||
/// Export del report come EMF (Enhanced Metafile) file.
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
private void Export(LocalReport report, string deviceInfo)
|
||||
{
|
||||
Warning[] warnings;
|
||||
m_streams = new List<Stream>();
|
||||
report.Render("Image", deviceInfo, CreateStream, out warnings);
|
||||
foreach (Stream stream in m_streams)
|
||||
{
|
||||
//stream.Flush();
|
||||
stream.Position = 0;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Export del report come PDF file.
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
private void ExportPDF(LocalReport report, string deviceInfo)
|
||||
{
|
||||
Warning[] warnings;
|
||||
m_streams = new List<Stream>();
|
||||
report.Render("PDF", deviceInfo, CreateStreamPdf, out warnings);
|
||||
foreach (Stream stream in m_streams)
|
||||
{
|
||||
//stream.Flush();
|
||||
stream.Position = 0;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// dispone l'applicazione e rilascia le risorse
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (m_streams != null)
|
||||
{
|
||||
foreach (Stream stream in m_streams)
|
||||
{
|
||||
stream.Close();
|
||||
}
|
||||
m_streams = null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// ciclo da fornire al renderizzatore dei report, per salvare 1 immagine da ogni pagina del report
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="fileNameExtension"></param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <param name="willSeek"></param>
|
||||
/// <returns></returns>
|
||||
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
||||
{
|
||||
// compongo stringhe x file con nomi univoci
|
||||
DateTime adesso = DateTime.Now;
|
||||
Random randNum = new Random();
|
||||
string filePathName = $"{baseDir}{localPdfPath}\\temp\\{name}_{adesso:HHmmss}_{adesso:ffffff}_{randNum.Next(0, 999):000}.{fileNameExtension}";
|
||||
Stream stream = new FileStream(filePathName, FileMode.Create);
|
||||
m_streams.Add(stream);
|
||||
return stream;
|
||||
}
|
||||
/// <summary>
|
||||
/// ciclo da fornire al renderizzatore dei report, per salvare 1 pdf da ogni pagina del report
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="fileNameExtension"></param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <param name="willSeek"></param>
|
||||
/// <returns></returns>
|
||||
private Stream CreateStreamPdf(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
||||
{
|
||||
// compongo stringhe
|
||||
DateTime adesso = DateTime.Now;
|
||||
string dailyDir = $"{baseDir}{localPdfPath}\\{adesso:yyyy}\\{adesso:MM}\\{adesso:dd}\\";
|
||||
string pdfPathName = $"{dailyDir}\\{name}_{adesso:HHmmss}_{adesso:ffff}.{fileNameExtension}";
|
||||
// creo Directory se non c'è
|
||||
fileMover fm = new fileMover(dailyDir, "");
|
||||
fm.checkDir();
|
||||
Stream stream = new FileStream(pdfPathName, FileMode.Create);
|
||||
m_streams.Add(stream);
|
||||
return stream;
|
||||
}
|
||||
/// <summary>
|
||||
/// Handler per PrintPageEvents
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="ev"></param>
|
||||
private void PrintPage(object sender, PrintPageEventArgs ev)
|
||||
{
|
||||
try
|
||||
{
|
||||
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
|
||||
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
|
||||
m_currentPageIndex++;
|
||||
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Instance.Error($"Errore in PrintPage{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// funzione di stampa...
|
||||
/// </summary>
|
||||
private bool Print(string printerName)
|
||||
{
|
||||
if (m_streams == null || m_streams.Count == 0)
|
||||
return false;
|
||||
|
||||
#endregion
|
||||
}
|
||||
bool fatto = false;
|
||||
// provo a stampare
|
||||
try
|
||||
{
|
||||
using (PrintDocument printDoc = new PrintDocument())
|
||||
{
|
||||
printDoc.PrinterSettings.PrinterName = printerName;
|
||||
if (!printDoc.PrinterSettings.IsValid)
|
||||
{
|
||||
Log.Instance.Error($"Impostazioni non valide per la stampante {printerName}");
|
||||
return fatto;
|
||||
}
|
||||
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
||||
printDoc.Print();
|
||||
fatto = true;
|
||||
};
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Instance.Error($"Errore in Print{Environment.NewLine}{exc}");
|
||||
}
|
||||
return fatto;
|
||||
}
|
||||
/// <summary>
|
||||
/// effettua pulizia della cartella temp x i files più vecchi di X ore (web.config)
|
||||
/// </summary>
|
||||
public void pulisciDir()
|
||||
{
|
||||
// num max ore di età x files "vecchi" da tenere in temp area...
|
||||
int maxAgeMinutes = memLayer.ML.CRI("maxAgeTempArea");
|
||||
int eliminati = 0;
|
||||
// ottengo elenco files *.emf
|
||||
fileMover.obj.setDirectory($"{baseDir}{localPdfPath}\\temp\\");
|
||||
FileInfo[] _fis = fileMover.obj.elencoFiles_FI("*.emf");
|
||||
bool fatto = false;
|
||||
foreach (FileInfo _file in _fis)
|
||||
{
|
||||
if (_file.CreationTime < DateTime.Now.AddMinutes(-maxAgeMinutes)) // elimino files vecchi...
|
||||
{
|
||||
fatto = fileMover.obj.eliminaFile(_file);
|
||||
if (fatto)
|
||||
{
|
||||
Log.Instance.Info($"Eliminato file {_file.Name}");
|
||||
eliminati++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// salvo il log degli update
|
||||
if (eliminati > 0)
|
||||
{
|
||||
Log.Instance.Info($"Eliminati {eliminati} files temporanei da area temp");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user