Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Mar 20, 2025 8:47 am

string html = "<html><body><h1>Test</h1><img src=\"file:///D:/Projects/Test/test/jpg_44-2.jpg\"></body></html>";
this is my test html string
I m testing in .net 8 console app in windows 64 environment
the pdf is generated but image file is not displaying
Last edited by vishnu23 on Thu Mar 20, 2025 11:44 am, edited 1 time in total.

vishnu23
 
Posts: 2
Joined: Thu Mar 20, 2025 8:39 am

Thu Mar 20, 2025 10:27 am

Hello,

Thanks for your inquiry.
Regarding your needs, we recommend using a new method to convert HTML to PDF files, which relies on the Chrome browser and is more stable and effective than Qt plugins. Please refer to this tutorial for testing. If you have any questions, please feel free to write back.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Thu Mar 20, 2025 11:48 am

Okay does that support giving html as string rather than file location

vishnu23
 
Posts: 2
Joined: Thu Mar 20, 2025 8:39 am

Fri Mar 21, 2025 2:02 am

Hello,

Thanks for your reply.
Apologies, this way does not support directly passing an HTML String. However, as a solution, you can write the HTML String to a temporary file and pass the path of the temporary file to the ConvertToPdf method. After the conversion is complete, you can delete the temporary file. I have provided the complete code for your reference.
Code: Select all
string html = "<html><body><h1>Test</h1><img src=\"file:///D:/Projects/Test/test/jpg_44-2.jpg\"></body></html>";
string outputFile = @"HtmlToPDF.pdf";

// Specify the path to the Chrome plugin
string chromeLocation = "";

// Create an instance of the ChromeHtmlConverter class
ChromeHtmlConverter converter = new ChromeHtmlConverter(chromeLocation);

// Create an instance of the ConvertOptions class
ConvertOptions options = new ConvertOptions();

// Set the conversion timeout
options.Timeout = 10 * 3000;
// Set the paper size and margins for the PDF
options.PageSettings = new PageSettings()
{
    PaperWidth = 8.27,
    PaperHeight = 11.69,
    MarginTop = 0,
    MarginLeft = 0,
    MarginRight = 0,
    MarginBottom = 0
};

// Create a temporary file path with the .html extension
string tempFilePath = Path.ChangeExtension(Path.GetTempFileName(), ".html");
try
{
    // Write the HTML string to the temporary file
    File.WriteAllText(tempFilePath, html);

    // Convert the HTML to PDF
    converter.ConvertToPdf(tempFilePath, outputFile, options);
}
finally
{
    // Delete the temporary file
    File.Delete(tempFilePath);
}

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF