Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while downloading html in pdf,by using third party tool to convert an html page to pdf. getting error- Conversion error: Could not open url

Tags:

c#

asp.net

pdf

while downloading html code into pdf in selectpdf software. im getting error saying - "Conversion error: Could not open url".im using selectpdf for converting html code to pdf. what is the base url i have to give .

using SelectPdf;
public partial class HtmlcodePrint : System.Web.UI.Page
{
    string TxtHtmlCode;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
             TxtHtmlCode = @"<html>
 <body>
  Hello World from selectpdf.com.
 </body>
</html>
";
        }
    }

    protected void Btndownloadpdf_Click(object sender, EventArgs e)
        {
            // read parameters from the webpage
            string htmlString = TxtHtmlCode;
            string baseUrl = "http://localhost:51868/HtmlcodePrint.aspx";

            string pdf_page_size ="A4";
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), 
                pdf_page_size, true);

            string pdf_orientation = "Portrait";
            PdfPageOrientation pdfOrientation = 
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), 
                pdf_orientation, true);

            int webPageWidth = 1024;
            try
            {
                webPageWidth = Convert.ToInt32("1024");
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32("777");
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.WebPageHeight = webPageHeight;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }
    }
like image 520
krishna mohan Avatar asked Nov 24 '25 14:11

krishna mohan


1 Answers

I know this is old, but I've been working with SelectPdf for a couple of days, so I'll throw in my 2 cents.

You probably don't need a baseUrl...

You don't have to give any baseUrl at all to the ConvertHtmlString function. You can just pass it the html string you want to convert and that's it.

Unless...

You only need to pass it a baseUrl if the html you're converting has relative paths in the external references (example: if you were referencing a stylesheet and wanted to use a relative path, you could provide the baseUrl to show where you wanted the stylesheet to be relative to). It's just so the converter can create the full absolute paths from the relative paths.

So...

If you don't need that functionality or just don't have external references in your html, then you can just use

converter.ConvertHtmlString(htmlString);

Also...

doc.Save(Response, false, "Sample.pdf");

may not be what you're looking for either. I only say this because the comments look like the same ones on the examples on the site for SelectPDF, so I'm assuming you copied the code from there (which is what I originally did too), in which case I want to let you know you don't have to save your PDF doc with that particular version of Save. It actually has 3 overloads to allow you to save your doc as:

  • a byte array (default)
  • a stream
  • a file
  • an HTTP response (the one you're using now, as shown in the examples from the site)

So, like I pointed out, you're using the one that saves the PDF as a HTTP response, so if you're wanting to save it as an actual PDF file directly, you'll need to change it to

doc.Save(fileName)

with the fileName variable as the absolute or relative path or file name you want to save the PDF to.

Hope this helps


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!