من با خیلی از این نرمافزار ها کار کردم.
تا اینکه سازنده یکی از اونها چند ماه قبل به من یه پیشنهاد جالب داد و گفت که خودش هم داره همین کار رو میکنه.
tomasz kubakki سازنده monoreports
پیشنهادشون طراحی چاپ با html بود و رندر گرفتن با یه انجین مثل phantomjs
اینها نمونه کدهای من هستن:
Stream configStream = fileInterfaceInstance.Read ("PhantomJS Prints" + System.IO.Path.DirectorySeparatorChar + "Configuration.xml");
System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer (typeof(PrintConfiguration[]), new []{ typeof(PrintConfiguration) });
PrintConfiguration[] config = (PrintConfiguration[])xmls.Deserialize (configStream);
foreach (var c in config) {
configs.Add (c.Name, c);
}
public override IAsyncResult BeginPrint (string name, System.Collections.Generic.IDictionary<string, SortedSet<DCBase>> tables, System.Collections.Generic.IDictionary<string, object> parameters, AsyncCallback asyncCallback, object asyncState)
string html;
if (!managers.ContainsKey (name)) {
if (!configs.ContainsKey (name)) {
throw new ArgumentException ("no tempelate defined for {0}", name);
} else {
var conf = fileInterfaceInstance.Read ("PhantomJS Prints" + System.IO.Path.DirectorySeparatorChar + configs [name].Path);
Console.WriteLine ("conf:{0}", conf.Length);
managers.Add (name, new PrintManager (conf));
}
}
html = managers [name].Compile (tables, parameters);
Console.WriteLine ("html:{0}", html.Length);
string binaryName;
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT)
binaryName = "phantomjs.exe";
else
binaryName = "phantomjs";
string dateTime = DateTime.Now.ToString ().Replace ("/", "_").Replace (":", "_");
Guid id = Guid.NewGuid ();
//string htmlFileName = "temp" + Path.DirectorySeparatorChar + "PhantomJS Print - " + name + " - " + dateTime + " - " + id.ToString () + ".html";
string pdfFileName = "temp" + Path.DirectorySeparatorChar + "PhantomJS Print - " + name + " - " + dateTime + " - " + id.ToString () + ".pdf";
//htmlFileName.Replace (" ", "");
//pdfFileName.Replace (" ", "");
//Console.WriteLine ("html:{0}", htmlFileName);
Console.WriteLine ("pdf:{0}", pdfFileName);
//System.IO.File.WriteAllText (htmlFileName, html, System.Text.Encoding.Unicode);
string arguments = "\"PhantomJS Prints/stdrasterize.js\" " + (parameters.ContainsKey ("Orientation") && Equals (parameters ["Orientation"], "landscape") ? "297mm*210mm" : "210mm*297mm") + " 1cm";
Console.WriteLine ("Arguments: {0}", arguments);
Process p = new Process ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = binaryName;
p.StartInfo.Arguments = arguments;
p.StartInfo.ErrorDialog = false;
p.StartInfo.RedirectStandardError = true;
p.Start ();
p.StandardInput.WriteLine (html);
p.StandardInput.Flush ();
p.StandardInput.Close ();
p.WaitForExit ();
var pdfStream = System.IO.File.OpenWrite (pdfFileName);
p.StandardOutput.BaseStream.CopyTo (pdfStream);
pdfStream.Close ();
public string Compile (System.Collections.Generic.IDictionary<string, SortedSet<DCBase>> tables, System.Collections.Generic.IDictionary<string, object> parameters)
{
var StaticParameters = CreateParameters ();
StringBuilder sb = new StringBuilder (printData, printData.Length * 2);
foreach (var kvp in StaticParameters)
sb.Replace ("$$" + kvp.Key + "", System.Web.HttpUtility.HtmlEncode (kvp.Value));
foreach (var kvp in parameters)
sb.Replace ("$$" + kvp.Key + "", System.Web.HttpUtility.HtmlEncode (kvp.Value.ToString ()));
string report = sb.ToString ();
foreach (var tp in tables) {
bool d = true;
while (d) {
int tableBeginOffset = report.IndexOf ("<$" + tp.Key + ">", StringComparison.Ordinal);
int tableEndOffset = report.IndexOf ("</$" + tp.Key + ">", StringComparison.Ordinal);
if (tableBeginOffset == -1) {
d = false;
continue;
}
if (tableEndOffset == -1) {
d = false;
continue;
}
string tableFields = report.Substring (tableBeginOffset, tableEndOffset - tableBeginOffset);
StringBuilder TBuilder = new StringBuilder ();
foreach (var row in tp.Value) {
string RowText = tableFields;
foreach (var column in row.Columns) {
RowText.Replace ("<$" + column + "/>", System.Web.HttpUtility.HtmlEncode (row.GetValue (column).ToString ()));
}
TBuilder.Append (RowText);
}
report = report.Substring (0, tableBeginOffset) + TBuilder + report.Substring (tableEndOffset + ("</$" + tp.Key + ">").Length);
}
}
return report;
}
var page = require('webpage').create(),
system = require('system'),
size;
fs = require('fs');
size = system.args[1].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: system.args[2] } : { format: system.args[1], orientation: 'portrait', margin: system.args[2] };
page.content = system.stdin;
window.setTimeout(function() {
page.render(system.stdout, { format: 'pdf' });
phantom.exit();
}, 1000);
نمونه فایل پرینت:
b3.phantom.zip
خیلی خلاصه, راحت شدم.
هر چیزی رو که نمیدونید چیه با DataRow/DataTable/... عوض کنید.