Skip to content Skip to sidebar Skip to footer

Html To Pdf With Images Using Hiqpdf

I am converting a html with images and css files using HiQPdf 8.0. I have done it lots of times in MVC 4. Now I am working under MVC 5 and HiQPdf 8.0 and the problem is HiQPdf 8.0

Solution 1:

Found some documentation you might enjoy here

The images and styles are missing from the PDF document generated by converter and the scripts are not executed because these elements are referenced by relative URLs in the HTML code I convert. How can I give a base URL to be used by converter to resolve these elements?

The methods to convert a HTML code of the HtmlToPdf, HtmlToImage and HtmlToSvg classes and the constructors of the PdfHtml and PdfHtmlImage classes have a baseUrl parameter that can be used by the HiQPdf HTML converters to resolve the relative URLs found in the HTML code you convert.

For example, if an image is referenced in the HTML code you convert with and the image can be accessed from the http://www.example.com/Images/image.png fully qualified URL, then you have to set http://www.example.com/ as base URL in your application code. Similar for the style and script files referenced in the HTML code you convert.

so it looks like youll have to use the fully qualified names to get it to work

Solution 2:

I`m adding my code snippet as the accepted answer is solving the issue, but it missing code example. File is located here in asp net core project

wwwroot/images/logo.png

c# code

        HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Landscape;

        // razor view to stringstring htmlToConvert = await RenderViewHelper.RenderViewAsync(context, "document_chtml", model);

        // add base url to constructorbyte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, $"{this.Request.Scheme}://{this.Request.Host}");

        Response.Headers.Add("Content-Disposition", "inline; filename=document.pdf");
        Response.ContentType = "application/pdf";
        Response.ContentLength = pdfBuffer.Length;

        returnnew FileContentResult(pdfBuffer, "application/pdf");

Razor code

<imgalt="logo"src="~/images/logo.png"title="logo"/>

Post a Comment for "Html To Pdf With Images Using Hiqpdf"