Manoj G
Manoj G

Reputation: 87

How to make pdf file size with respect to div height and width using wkhtmltopdf.exe

I tried to make pdf with wkhtmltopdf.exe by supplying html string and is working properly.But i want to provide pdf custom size to exe.

eg:- Suppose i have a div of height 30 and width 50. then the generated pdf should be of same size.

Below is the code which i found from this website forum

     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

Upvotes: 0

Views: 2520

Answers (1)

Pieter
Pieter

Reputation: 1774

If you want to use a standard paper size you can use the --page-size argument and provide the page size name (A4, Letter, etc.). If you want a custom page size, you can use the --page-width and --page-height arguments (e.g. --page-width 100mm --page-height 200mm).

By default, wkhtmltopdf will add a margin around each side of the page, which is used to render the header and footer in. To change the page margins, use the arguments --margin-top, --margin-right, --margin-bottom and --margin-left.

Another thing to note is the --disable-smart-shrinking; by default, wkhtmltopdf will 'guess' the dpi of the pdf by calculating the html widths and trying to fit it within the boundaries of the pdf page. If you want to precisely measure each element on the page, you should add --disable-smart-shrinking to your argument list. However, make sure the html output defines the exact size of the html page (which should be the same as the pdf page minus the page margins), otherwise wkhtmltopdf might not be able to render the page properly.

Upvotes: 2

Related Questions