wkhtmltopdf non prenderà immagini all'interno dei file html
-
28-10-2019 - |
Domanda
Stiamo usando wkhtmltopdf (in wkhtmltoxsharp lib) prendendo file .html locali e convertendoli in file .pdf.
All'interno dell'HTML, abbiamo un collegamento Web a un'unità di rete con un'immagine .gif. Quando si converte .pdf esce senza l'immagine.
Qualche idea su come raggiungere questo obiettivo?
Soluzione
La versione Windows di WKHTMLTOPDF 0.11.0 RC1 non supporta le immagini GIF. Soluzione alternativa: preelaborare tutte le immagini in HTML in arrivo e convertirle in JPG.
Problema descritto in: http://code.google.com/p/wkhtmltopdf/issues/detail?id=441
Le GIF funzionano in wkhtmltopdf 0.9.9.
Altri suggerimenti
So che è già una risposta, ma penso che qualcuno possa usare anche questo pezzo di informazione: sto lavorando con la versione 1.1.4 0.10 e i Potere Stampa gif, jpgs ecc.
Ma quello che potrei non Mettiti al lavoro è avere la pagina locale (che lavora a http://localhost/
) Converti le immagini che hanno la loro fonte impostata con il percorso relativo.
Quindi non ha funzionato con questo:<img src="../somepath/echoimage.php?params" >
Ho stanco diversi tipi di immagini con quella soluzione sopra da allora, come è mostrato ad esempio, ho usato immagini dinamiche, ma nessuna di quelle mi ha aiutato.
Alla fine ha funzionato src
Avere il percorso assoluto, come qui:<img src="http://localhost/fullpath/echoimage.php?params" >
Non avevo il tempo di testare sarebbe stato il primo scenario se fosse stato dato altezza/larghezza o se non fosse l'immagine dinamica. Impostare il percorso assoluto era accettabile in questo caso, quindi mi sono fermato lì.
Basta usare il codice seguente che ti modificherà HTML SRC dal mappato al percorso assoluto e otterrai l'immagine. wkhtmltopdf prende jpeg e gif entrambi i tipi di immagine
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
Se hai definito il codice delle immagini in CSS, quindi rimuovi quei codice e otterrai il PDF con le immagini.