6

任意の html ファイルから wkhtmltopdf によって作成された pdf ストリームを取得し、IE/Firefox/Chrome などでダウンロード ダイアログをポップアップ表示することは可能ですか?

現時点では、次のコードで出力ストリームを取得しています。

public class Printer
{
    public static MemoryStream GeneratePdf(StreamReader Html, MemoryStream pdf, Size pageSize)
    {
        Process p;
        StreamWriter stdin;
        ProcessStartInfo psi = new ProcessStartInfo();

        psi.FileName =  @"C:\PROGRA~1\WKHTML~1\wkhtmltopdf.exe";

        // run the conversion utility 
        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        // note that we tell wkhtmltopdf to be quiet and not run scripts 
        psi.Arguments = "-q -n --disable-smart-shrinking " + (pageSize.IsEmpty ? "" : "--page-width " + pageSize.Width + "mm --page-height " + pageSize.Height + "mm") + " - -";

        p = Process.Start(psi);

        try
        {
            stdin = p.StandardInput;
            stdin.AutoFlush = true;
            stdin.Write(Html.ReadToEnd());
            stdin.Dispose();

            CopyStream(p.StandardOutput.BaseStream, pdf);
            p.StandardOutput.Close();
            pdf.Position = 0;

            p.WaitForExit(10000);

            return pdf;
        }
        catch
        {
            return null;
        }
        finally
        {
            p.Dispose();
        }
    }

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }
    }
}

次に、ダイアログを表示します。

MemoryStream PDF = Printer.GeneratePdf(Rd, PDFStream, Size);

byte[] byteArray1 = PDF.ToArray();
PDF.Flush();
PDF.Close();
Response.BufferOutput = true;

Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray1);
Response.End();

PDF ファイルから作成された MemoryStreams では問題なく動作しますが、ここでは空のページしか取得できません。bytearray には 1270 バイトがあります。

4

2 に答える 2

4

これはまだ問題がありますか?

wkhtmltopdf 0.11.0 rc2 をインストールした後、コンピューターでこれをテストするために新しい ASP.net Web サイトを作成したところ、PDF の作成に問題なく機能しました。私のバージョンはわずかに異なっていました。

私のCSHTMLには次のものがありました:

MemoryStream PDFStream = new MemoryStream();
MemoryStream PDF = Derp.GeneratePdf(PDFStream);
byte[] byteArray1 = PDF.ToArray();
PDF.Flush();
PDF.Close();
Response.BufferOutput = true;
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray1);
Response.End();

私のダープクラス

public class Derp
{
    public static MemoryStream GeneratePdf(MemoryStream pdf)
    {
        using (StreamReader Html = new StreamReader(@"Z:\HTMLPage.htm"))
        {
            Process p;
            StreamWriter stdin;
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = @"C:\wkhtmltopdf\wkhtmltopdf.exe";
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.Arguments = "-q -n --disable-smart-shrinking " + " - -";
            p = Process.Start(psi);
            try
            {
                stdin = p.StandardInput;
                stdin.AutoFlush = true;
                stdin.Write(Html.ReadToEnd());
                stdin.Dispose();
                CopyStream(p.StandardOutput.BaseStream, pdf);
                p.StandardOutput.Close();
                pdf.Position = 0;
                p.WaitForExit(10000);
                return pdf;
            }
            catch
            {
                return null;
            }
            finally
            {
                p.Dispose();
            }
        }
    }

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }
    }
}
于 2012-08-16T16:31:22.967 に答える
0

@Nenotlepの回答に基づいたこれに対する私の見解。これはpdf生成部分のみです。

私は非同期を使用しています。wkhtmltopdf はデフォルトで utf-8 を想定しているため、新しい StreamWriter を作成しましたが、プロセスの開始時に別の値に設定されています。

p.WaitForExit(...) を削除したのは、失敗した場合に処理していなかったため、いずれにせよハングするからですawait tStandardOutput。タイムアウトが必要な場合は、キャンセルトークンまたはタイムアウトを使用してさまざまなタスクで Wait を呼び出し、それに応じて処理する必要があります。

public static async Task<byte[]> GeneratePdf(string html, Size pageSize)
{
    ProcessStartInfo psi = new ProcessStartInfo
    {
        FileName = @"C:\PROGRA~1\WKHTML~1\wkhtmltopdf.exe",
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        Arguments = "-q -n --disable-smart-shrinking " 
            + (pageSize.IsEmpty ? "" : "--page-width " + pageSize.Width 
            + "mm --page-height " + pageSize.Height + "mm") + " - -";
    };

    using (var p = Process.Start(psi))
    using (var pdfSream = new MemoryStream())
    using (var utf8Writer = new StreamWriter(p.StandardInput.BaseStream, 
                            Encoding.UTF8))
    {
        await utf8Writer.WriteAsync(html);
        utf8Writer.Close();
        var tStdOut = p.StandardOutput.BaseStream.CopyToAsync(pdfSream);
        var tStdError = p.StandardError.ReadToEndAsync();

        await tStandardOutput;
        string errors = await tStandardError;

        if (!string.IsNullOrEmpty(errors))
        {
            //deal with errors
        }

        return pdfSream.ToArray();
    }
}

そこには含まれていませんが、参照として役立つ可能性があるもの:

  • --cookie を使用して、必要に応じて認証 Cookie を渡すことができます。
  • HTML ページの追加リクエスト (CSS、画像など) に対して、HTML ページのサーバーを指す href を使用してベース タグを設定できます。
于 2018-11-27T21:33:11.887 に答える