このツールが URL を検索し、応答を pdf に変換することは知っています。を変換するにはどうすればよいですか
<html> content.. </html>
pdfに?
wkhtml2pdf のヘルプ ファイルを見ていましたが、stdin のオプションが提供されているように見えますが、stdin をエミュレートする方法がわかりません。
また、より良い仕事をするツールを知っているなら、私にいくつか提案してください。
どうもありがとう!
このツールが URL を検索し、応答を pdf に変換することは知っています。を変換するにはどうすればよいですか
<html> content.. </html>
pdfに?
wkhtml2pdf のヘルプ ファイルを見ていましたが、stdin のオプションが提供されているように見えますが、stdin をエミュレートする方法がわかりません。
また、より良い仕事をするツールを知っているなら、私にいくつか提案してください。
どうもありがとう!
wkhtmltopdf の C# P/Invoke ラッパーを提供する新しいプロジェクトを開始しました。
私のコードはhttps://github.com/pruiz/WkHtmlToXSharpでチェックアウトできます。
挨拶します。
wkhtmltopdf は無料のツールですが、.NET で記述されていないため、asp.net アプリケーションに統合するのが難しい場合があります。
無料ですが、どのような種類の html も処理できないiTextSharpを見ることができます。または、 ExpertPDFやABCpdfなど、任意の html/css を処理できる html を pdf に変換する商用ツールを見ることができます。
私は方法を発見しました。通常のhtmlを出力するように別のものを設定できます。その URL を wkhtml2pdf プロセスの入力値として使用します。
- - - - - 編集
public byte[] WKHtmlToPdf(string url_input)
{
try
{
var fileName = " - ";
var wkhtmlDir = ConfigurationSettings.AppSettings["wkhtmlDir"];
var wkhtml = ConfigurationSettings.AppSettings["wkhtml"];
var p = new Process();
string url = Request.Url.GetLeftPart(UriPartial.Authority) + @"/application/" + url_input;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = wkhtml;
p.StartInfo.WorkingDirectory = wkhtmlDir;
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
switches += "--page-size Letter ";
p.StartInfo.Arguments = switches + " " + url + " " + fileName;
p.Start();
//read output
byte[] buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
break;
}
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
return returnCode == 0 ? file : null;
}
catch (Exception ex)
{
// set your exceptions here
return null;
}
}
----------web.config キーの例
<add key="wkhtmlDir" value="C:\Program Files (x86)\wkhtmltopdf\bin"/>
<add key="wkhtml" value="C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe"/>
基本的な考え方は、url を引数として exe に渡すことです。
HTH !