We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert html to PDF missing image

I have a image with virtual path in html, after I convert the html to PDF, I can't find the image in generated PDF file. I do need the virtual path for images in html, how can I get it work? Thanks.

Hi Tom,

Thanks for your interest in our products.

Can you please share some details regarding sample source code and template HTML file you are using or create a sample application to show the issue. This will help us to figure out the issue and reply back to you soon.

We apologize for your inconvenience.

Thanks & Regards,

Thanks for your response. By the way, the full web url works for me.

Sample code:

// create a Web Request object to connect to remote URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// set the Web Request timeout
request.Timeout = 10000; // 10 secs

// Retrieve request info headers
HttpWebResponse localWebResponse = (HttpWebResponse)request.GetResponse();
// Windows default Code Page (Include System.Text namespace in project)
Encoding encoding = Encoding.GetEncoding(1252);
// Read the contents of into StreamReader object
StreamReader localResponseStream = new StreamReader(localWebResponse.GetResponseStream(), encoding);

// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, localResponseStream.ReadToEnd());

// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;

// Add the text object containing HTML contents to PD Sections
section.Paragraphs.Add(text2);

// Specify the URL which serves as images database
//pdf.HtmlInfo.ImgUrl = "http://en.wikipedia.org/ 11";
//Save the pdf document
//pdf.Save("D:/pdftest/DirectHTML2pdf.pdf");

pdf.Save(newFilePath);

localWebResponse.Close();
localResponseStream.Close();

Part of html:

No image

This link works.

This link doesn’t work.

Hi Tom,

Thanks for sharing the sample code.

You can get the application root path value by using ~ character. I have tested the scenario in which I have stored an image in "images" folder which is present at my application root path. I have specified the ImgUrl using following code line and the Pdf is being generated correctly.

[C#]
// Define the path for the Image files being used by the HTML file
pdf.HtmlInfo.ImgUrl = "~/images";

Can you please try using this approach and in case you still face any problem, please share the source HTML file with us or create a sample application to show the issue. This will help us to figure out the issue and reply back to you soon.

We apologize for your inconvenience.

Thanks & Regards,

The problem still exists.

I put the sample site code and the generated PDF file to attached file below.

Open GetFile.aspx will generate the sample PDF file in folder C:\inetpub\wwwroot\article_pdfs\.

Physical path: C:\inetpub\wwwroot\image
Virtual directory: images

Hi Tom,

Thank you for sharing the details with us. I tested the scenario and able to notice the same problem. For rectification, I logged this problem with ID: PDFNEWNET-33346 in our Issue Tracking System. We will further look into the details of this issue and will keep you updated via this forum thread on the status of correction.

We apologize for your inconvenience.

Thanks & Regards,

I can't see the CSS supported in converting html to pdf, is this true?

Hi Tom,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Aspose.Pdf for .NET HTML2PDF conversion does support CSS. If your HTML file contains CSS, Aspose.Pdf for .NET will render the styles in PDF file. If you are facing any issue, please share some details with us (with your sample file). We will check it and get back to you.

Thank You & Best Regards,

Great. I still can't get the external CSS work for html2pdf conversion. I send my sample code to you, please help me out, thanks a lot.

HTML : Print.aspx

CSS file: Site.css

Hi Tom,

Thanks for sharing the sample application with us. I tested the scenario but unable to find any issue in your provided application. External CSS file "Site.css" applied properly in "Print.aspx" page. Screenshot is attached for your reference.

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Thanks for you response.

The attached PDF file is generated based on "Print.aspx".

Please remove the PDF file in the sample project and open "GetFile.aspx" to generate a new one.

You will see the missing styles.

Hi Tom,

Thanks for sharing the details with us. I tested the scenario and able to notice the same problem. For rectification, I logged this problem with ID: PDFNEWNET-33377 in our Issue Tracking System. We will further look into the details of this issue and will keep you updated via this forum thread on the status of correction.

We apologize for your inconvenience.

Thanks & Regards,

The issues you have found earlier (filed as PDFNEWNET-33346) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

I have the evaluation copy of the Apose PDF .net 8.5 and am trying to convert html to pdf . An image is lost in the conversion. How can I include images? Thanks

Hi there,


Thanks for your inquiry. You need to set ImgUrl property for image paths. ImgUrl property indicates the image URL which serves as a form of database when converting HTML to Pdf.

Pdf.HtmlInfo.ImgUrl provides the basic path of image and the property ‘src’ of provides the relative path. The ultimate path of image in html is equal to Pdf.HtmlInfo.ImgUrl concatenated with the src of in html source.


// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(@“D:/pdftest/HTML2pdf.html”);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);
// Specify the URL which serves as images database
pdf.HtmlInfo.ImgUrl = “D:/pdftest/MemoryStream/”;

//Save the pdf document
pdf.Save(“D:/pdftest/MemoryStream/HTML2pdf.pdf”);

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for your reponse. It looks pretty straight forward, but it is still not working.
If URL for the actual images is http://testweb/images/logo.gif

Could pdf.HtmlInfo.ImgUrl="http://testweb/images/"

and html

Hi Ann,


Thanks for your inquiry. Yes you are right if you provide relative path in tag then final path will be combination of ImgUrl and src property. If you provide absolute path in tag then you don’t need to use ImgUrl property. Hopefully it will help you to accomplish the task. If you face any issue then please share more details so we will investigate the issue and will provide you more information accordingly.

Best Regards,

Hi,


Thanks for your patience.

We have further investigated the issue PDFNEWNET-33377 reported earlier. Please note that we have fixed the case for Print.aspx file. However we do not see any css that should be applied when GetFile.aspx is loaded. Can you please take a look over this matter.

I am having trouble with a similar issue. I’m using the Pdf.BindHtml method with a Stream object to convert my HTML document to PDF. However, it fails to pull back any images, despite all img src attributes being absolute URLs (e.g. my HTML is being retrieved through a web request to “http://www.mysite.com/api/Item/1/” and my image tag is <img src="http://www.mysite.com/api/1/image.jpg"/>). Is there any reason this wouldn’t be working? I know that I can make this work using a custom image loader if I use a Pdf.Document type to convert to PDF, but I don’t see an HtmlLoadOptions property on the Generator.Pdf type to do the same thing.

Hi there,


Thanks for your inquiry. Please note Aspose.Pdf.Generator namespace is obsolete now. We are making fixes and enhancement in new generator(Aspose.Pdf.Document). Please use new approach for HTML to PDF conversion, you may use basepath for images database. Hopefully it will help you to accomplish the task. If issue persist the please share your sample HTML file, we will test the scenario at our end and will provide you information accordingly.


Please feel free to contact us for any further assistance.

Best Regards,