开发者

wkhtmltopdf wont take images inside html files

We are using wkhtmltopdf (in the wkhtmltoxsharp lib) by taking local .htm开发者_如何学Gol files and converting them to .pdf files.

Inside the HTML, we have a web link to a network drive with a .gif image. when converting the .pdf comes out without the image.

Any thoughts on how to achieve this?

More discussion on this at Google Code.


I know this is answered already, but I think someone may use this piece of info too: I'm working with version 1.1.4 0.10 and I can print GIFs, JPGs etc.

But, what I could not get to work is have the local page (working at http://localhost/) convert images that have their source set up with relative path.

So it didnt work with this: <img src="../somepath/echoimage.php?params" >

I tired several image types with that solution above since, as it's shown in example, I used dynamic images, but none of those helped.

It did work in the end with src having the absolute path, like here: <img src="http://localhost/fullpath/echoimage.php?params" >

I didn't have time to test would possibly the first scenario work if height/width were given or if it wasn't the dynamic image. Setting the absolute path was acceptable in this case so I stopped there.


The windows version of wkhtmltopdf 0.11.0 rc1 does not support gif images. Workaround: preprocess all images in incoming HTML and convert them into jpgs.

Issue described in: http://code.google.com/p/wkhtmltopdf/issues/detail?id=441

Gifs work in wkhtmltopdf 0.9.9.


Just use the below code it will modify you html src from mapped to absolute path and you will get the image. wkhtmltopdf takes jpeg and gif both types of image

  Public Function getImage(ByVal input As String) As String
    If input Is Nothing Then
        Return String.Empty
    End If
    Dim tempInput As String = input
    Dim pattern As String = "<IMG(.|)+?>"
    Dim src As String = String.Empty
    Dim context As HttpContext = HttpContext.Current
    'Change the relative URL's to absolute URL's for an image, if any in the HTML code. 
    For Each m As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase Or RegexOptions.Multiline Or RegexOptions.RightToLeft)
        If m.Success Then
            Dim tempM As String = m.Value
            Dim pattern1 As String = "src=['|""](.+?)['|""]"
            Dim reImg As New Regex(pattern1, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
            Dim mImg As Match = reImg.Match(m.Value)
            If mImg.Success Then
                src = mImg.Value.ToLower().Replace("src=", "").Replace("""", "")
                If src.ToLower().Contains("http://") = False Then
                    'IIf you want to access through you can use commented src line below 
                    '   src = "src=\"" + context.Request.Url.Scheme + "://" + context.Request.Url.Authority + "/" + src + "\"";
                    src = "src=""" & Server.MapPath("~") & "\" & src & """"


                    Try
                        tempM = tempM.Remove(mImg.Index, mImg.Length)
                        tempM = tempM.Insert(mImg.Index, src)
                        'insert new url img tag in whole html code 
                        tempInput = tempInput.Remove(m.Index, m.Length)
                        tempInput = tempInput.Insert(m.Index, tempM)
                    Catch e As Exception
                    End Try
                End If
            End If
        End If
    Next
    Return tempInput
End Function


If you have defined Images code into css so remove those code and you will get PDF with images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜