2011-12-24 24 views
0

html文字列を入力してwkhtmltopdf.exeでpdfを作ろうとしましたが、正しく動作しています。wkhtmltopdf.exeを使ってdivの高さと幅に関してpdfファイルサイズを作る方法

例: - 高さ30と幅50のdivがあるとします。生成されるpdfのサイズは同じでなければなりません。以下は

あなたは--page-size引数を使用して、ページサイズ名を指定することができ、標準の用紙サイズ(A4を、使用したい場合、私はこのウェブサイトのフォーラム

 Private Sub WritePDF(ByVal HTML As String) 
    Dim inFileName As String, outFileName As String, tempPath As String 
    Dim p As Process 
    Dim stdin As System.IO.StreamWriter 
    Dim psi As New ProcessStartInfo() 

    tempPath = Server.MapPath("~") + "\Uploads\" 
    inFileName = Session.SessionID + ".htm" 
    outFileName = Session.SessionID + ".pdf" 

    ' run the conversion utility 
    psi.UseShellExecute = False 
    'psi.FileName = "c:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe" 
    psi.FileName = Server.MapPath("~") + "\App_Data\wkhtmltopdf.exe" 
    psi.CreateNoWindow = True 
    psi.RedirectStandardInput = True 
    psi.RedirectStandardOutput = True 
    psi.RedirectStandardError = True 


    ' note that we tell wkhtmltopdf to be quiet and not run scripts 
    ' NOTE: I couldn't figure out a way to get both stdin and stdout redirected so we have to write to a file and then clean up afterwards 
    psi.Arguments = "-q -n - " & tempPath & outFileName 

    p = Process.Start(psi) 

    Try 
     stdin = p.StandardInput 
     stdin.AutoFlush = True 

     stdin.Write(HTML) 
     stdin.Close() 

     If p.WaitForExit(15000) Then 
      ' NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works 
      'Response.WriteFile(tempPath + outFileName); 
      Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath & outFileName)) 
     End If 
    Finally 
     p.Close() 
     p.Dispose() 
    End Try 

    ' delete the pdf 
    System.IO.File.Delete(tempPath & outFileName) 
End Sub 
+0

jqueryからデータを送信しているためです。だから私はjqueryにタグを付けるべきだと思った。上記の言語はvb.netです –

答えて

2

から見つかったコードですレターなど)。カスタムページサイズが必要な場合は--page-widthと - page-height引数を使用できます(例:--page-width 100mm - page height 200mm)。

デフォルトでは、wkhtmltopdfはヘッダーとフッターを表示するためにページの両側に余白を追加します。ページの余白を変更するには、引数--margin-top、 - margin-right 、 - margin-bottom、 - margin-leftです。

もう1つ注目すべきは、--disable-smart-shrinkingです。デフォルトではwkhtmltopdfはhtmlの幅を計算してp​​dfページの境界内に収めることでpdfのdpiを '推測'します。ページ上の各要素を正確に測定したい場合は、引数リストに--disable-smart-shrinkingを追加する必要があります。ただし、html出力がhtmlページの正確なサイズを定義していることを確認してください(pdfページからページの余白を差し引いたものと同じにする必要があります)。そうしないと、wkhtmltopdfがページを適切にレンダリングできないことがあります。

関連する問題