多くのフォーラムやブログで話題になっていることは知っています。私は多くの記事を読みました。そして、それらの多くは静かに有益です。しかし、私にとっては、このタスクを達成するには新しいアプローチが必要なようです。
サーバー側でhtmlを印刷するソリューションを探しています。しかし、多くのオプションを検討した結果、
- プリンター名を指定できない、または
- txtファイルのようなHTML生コンテンツの印刷
後で、ghostscript ( https://stackoverflow.com/a/2600189/1238159 ) を使用してサーバー側で PDF をサイレントに印刷できることを知りました。
また、crystal report (ただし、多くのタグをサポートしていなくても HTML コンテンツを動的に渡す方法)、itextsharp、ssrs、pdfsharp などを試してみましたが、HTMl タグと W3C 標準の多くをサポートしているものはありませんでした。だから私はPDFを生成するためにある時点で終了しました。html を pdf に変換するのに最適なのは wkhtmltopdf だけです。私の経験では、他とは異なり、すべての html タグをサポートしています。しかし、PDF を印刷することは長年の私にとっての疑問です。
しかし、現在、GhostScript でも問題に直面しています (ver 9.05 を使用しています)。localhost を使用すると、完全に使用できます。サーバー側で、UIからサイレントに取得されたプリンター名に印刷できます。しかし、IPアドレスまたはマシン名では機能しません。偽装も実装しました。GhostScript の呼び出し中にプロセスがハングしても。
今、はっきりさせたいのは
- サーバー側で html または pdf (実際のコンテンツ) を印刷することは可能ですか?
- これを達成するためのオープンソースツールはすべて存在します
- 動的に渡したいプリンター名
手がかりや回避策は、世界中の何時間もの人々を助けるかもしれません. :)
よろしくお願いします。
よろしく、パバンN
ラウによる提案を使用した後。コマンドプロンプトで実行できます(cmd.exeが私のアカウントで実行されることを意味します)。しかし、私のアプリケーションはネットワーク サービスで実行されます。今、このACCESS Deniedのような問題が発生しています
うん。最後に、プロセスを開始することができました。ドメイン資格情報を使用して、タスクマネージャーの下に gswin32c.exe プロセスを表示できます。コードは次のとおりです。
public bool PrintVSPDF(string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName)
{
Logger.AddToLog("printerName", printerName);
string impersonationUsername = "";
string impersonationDomain = "";
string impersonationPWD = "";
if (ConfigurationManager.AppSettings["UName"] != null)
{
impersonationUsername = Encryption.Decrypt(ConfigurationManager.AppSettings["UName"].ToString(), Encryption.DEFAULT_KEY, Encryption.DEFAULT_SEED);
impersonationDomain = impersonationUsername.Split('\\').Count() > 1 ? impersonationUsername.Split('\\')[0] : "";
impersonationUsername = impersonationUsername.Split('\\').Count() > 1 ? impersonationUsername.Split('\\')[1] : impersonationUsername.Split('\\')[0];
}
if (ConfigurationManager.AppSettings["PD"] != null)
{
impersonationPWD = Encryption.Decrypt(ConfigurationManager.AppSettings["PD"].ToString(), Encryption.DEFAULT_KEY, Encryption.DEFAULT_SEED);
}
using (Impersonation imp = new Impersonation(impersonationUsername, impersonationDomain, impersonationPWD))
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = "-dPrinted -dNoCancel -dNOPAUSE -dBATCH -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=mswinpr2 -sOutputFile=%printer%\"" + printerName + "\" \"" + pdfFileName + "\" ";
startInfo.FileName = ghostScriptPath;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
//startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UserName = impersonationUsername;
startInfo.Domain = impersonationDomain;
SecureString ss = new SecureString();
for (int i = 0; i < impersonationPWD.Length; i++)
{
ss.AppendChar(impersonationPWD[i]);
}
startInfo.Password = ss;
Process process = null;
try
{
process = Process.Start(startInfo);
//Logger.AddToLog("Error VS", process.StandardError.ReadToEnd());
//Logger.AddToLog("Output VS", process.StandardOutput.ReadToEnd());
//Logger.AddToLog(process.StartInfo.Arguments.ToString(), "VS Print Arguments");
//Console.WriteLine(process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd());
//Logger.AddToLog(process.StartInfo.FileName.ToString(), "VS Print file name");
process.WaitForExit(30000);
if (process.HasExited == false)
process.Kill();
int exitcode = process.ExitCode;
process.Close();
return exitcode == 0;
}
catch (Exception ex)
{
Logger.AddToLog(ex);
return false;
}
}
}
しかし、私のビジュアルスタジオから実行している間、プロセスはlocalhost:5030で完全に機能しています。ただし、IP アドレスまたはマシン名を使用します。ハングしてこのエラーをスローするだけです
Adobe Reader、foxitなどでも同じことが起こっています。
( Process must exit before requested information can be determined. : at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.get_ExitCode() )