We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Is it possible to convert an html string to PDF in XF

Hello,

I'm using XAmarin.Forms and just wanted to know if it's possible to convert an html string to PDF (not a URL to pdf, just an HTML string) using syncfusion?

Thanks,

Reihaneh


12 Replies

KK Karthik Krishnaraj Syncfusion Team December 11, 2018 12:43 PM UTC

Hi Reihaneh,  
 
Thanks for contacting Syncfusion support.   
 
Currently we do not have native support for converting URL/ HTML string to PDF in Xamarin Forms. As a workaround, we can convert URL/HTML string to PDF in Xamarin using Web Service. Please refer below KB link for sample and code snippet for converting HTML to PDF in Xamarin using web service,     
KB:  
 
Kindly refer below UG and KB links for converting HTML string to PDF.    
UG:  
KB:  
 
Please let us know if you need any further assistance on this.   
 
Regards, 
Karthik. 



RK Reihaneh Khaksaran December 15, 2018 03:59 PM UTC

Hi,

Thank you so much for your reply, one more question, what dlls should I add to my project? I mean according to the samples, I must add Syncfusion.Pdf.Base, Syncfusion.Compression.Base and Syncfusion.HtmlConverter.Base, is thath coorect? and I also have installed Syncfusion Essential Studio, but I can't find any of these dlls anywhere in my computer, is there anything else I should download and install?

Thanks,
Reihaneh


KC Karthikeyan Chandrasekar Syncfusion Team December 17, 2018 06:26 AM UTC

Hi Reihaneh, 
Yes, you should install the WebKit installer to get the required assemblies. Please refer the prerequisites to get the details. 

Revert me back if you need any additional information. 
Regards, 
Karthikeyan  



RK Reihaneh Khaksaran December 17, 2018 01:53 PM UTC

Hi,

Thank you, I downloaded and added the following assemblies:

Syncfusion.Compression.Portable
Syncfusion.HtmlConverter.Portable
Syncfusion.Pdf.Portable

but after running the code below,

        public MemoryStream CreateDocument(string pathURL)
        {
            HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

            //Initialize the WebKit settings
            WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

            //Set the webKit path
            webKitSettings.WebKitPath = "../QtBinaries/";

            //Set the WebKit conversion settings
            htmlConverter.ConverterSettings = webKitSettings;

            //Convert url to pdf
            MainPage.stateSequencerController.GenerateHTML();
            PdfDocument document = htmlConverter.Convert(pathURL);

            //Save the document
            MemoryStream stream = new MemoryStream();

            document.Save(stream);

            //Sets the stream position
            stream.Position = 0;

            //Close the PDF document
            document.Close();

            //Return the PDF stream.
            return stream;

        }

I get this exception:
Unhandled Exception:

System.TypeLoadException: Could not load type of field 'Syncfusion.HtmlConverter.WebKitConverterSettings:m_toc' (18) due to: Could not resolve type with token 01000011 (from typeref, class/assembly Syncfusion.Pdf.HtmlToPdf.HtmlToPdfToc, Syncfusion.Pdf.Portable, Version=16.4200.0.40, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89) assembly:Syncfusion.Pdf.Portable, Version=16.4200.0.40, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89 type:Syncfusion.Pdf.HtmlToPdf.HtmlToPdfToc member:(null) signature:<none> occurred

Can you please help me with this?

Thanks,
Reihaneh


KC Karthikeyan Chandrasekar Syncfusion Team December 18, 2018 11:13 AM UTC

Hi Reihaneh 
As mentioned in our earlier update () we do not have support for HTML to PDF in Xamarin platform. You can use it only by creating the web service of your own. Web service will be created in .NET Framework and the endpoints will be used for the conversion in Xamarin application. Please refer the KB article for the samples. The following assemblies should be referred in the Web Service application. 
Syncfusion.Compression.Base 
Syncfusion.HtmlConverter.Base 
Syncfusion.Pdf.Base 

Regards, 
Karthikeyan  



RK Reihaneh Khaksaran December 24, 2018 02:14 PM UTC

Hi,

So all I need to do is create a web service? Is there any guide on that?

Thank you


KC Karthikeyan Chandrasekar Syncfusion Team December 26, 2018 04:42 AM UTC

Hi Reihaneh, 
Yes, you need to create a web service, please find the working sample in the below link. 
 
Revert me back if you need any further assistance in this. 
Regards, 
Karthikeyan  



RK Reihaneh Khaksaran January 28, 2019 09:47 AM UTC

Hi,

I have downloaded this sample

but when I try to run it, I get the following exception:

Unhandled Exception:
System.Net.WebException: Error: ConnectFailure (Connection refused) occurred

Could you please tell me what I'm doing wrong?

Best regards,
Reihaneh


RK Reihaneh Khaksaran replied to Reihaneh Khaksaran February 2, 2019 06:59 AM UTC

Hi,

I have downloaded this sample

but when I try to run it, I get the following exception:

Unhandled Exception:
System.Net.WebException: Error: ConnectFailure (Connection refused) occurred

Could you please tell me what I'm doing wrong?

Best regards,
Reihaneh

Hi,

Could you please help me with this issue?

Thank you


KC Karthikeyan Chandrasekar Syncfusion Team February 4, 2019 09:11 AM UTC

Hi Reihaneh, 

This exception may occurs if the service is not reachable from the device. If you are using local host URL as web reference in the Xamarin application then this exception may occurs due to the service is not reachable. If you are using localhost URL, then the device will actually trying to connect the device itself not to your desktop/server. If you want to connect the service from desktop/server, then you to host the service in the desktop/server then you need to use the IP address of the target machine.  

Note: We have ensured this by hosting the service in IIS and then provided the complete service URL in the Xamarin application.  
Regards, 
Karthikeyan  



DI Dinesh December 11, 2019 09:44 AM UTC

Hi Team,

I have downloaded this sample
 


but when I try to run it, I get the following exception:

Unhandled Exception:
System.Net.WebException: Error: ConnectFailure (Connection refused) occurred

Could you please tell me what I'm doing wrong?

Best regards,
Reihaneh

I am getting the same error in the demo application. I have used normal string as like below which does not need to connect to server.

For example :

<html>
    <head>
        <meta charset="UTF-8">
        <title>{{Test4}}</title>
    </head>
    <body>
        <h1>{{Test4}}</h1>
        <hr />
        <ul>
            <li>{{Test4}}</li>
            <li>{{Test4}}</li>
            <li>{{Test4}}</li>
        <hr />
        <h2>Embedded Data here:</h2>
        <table>
 
    <tr><th>Test_Currency</th><th>TIME01</th><th>Test4</th><th>Test4</th></tr>     
</table>
    </body>
</html>

And about the below line. I don't know what does it mean. Am new to this. Please help me to sort this.

Note: We have ensured this by hosting the service in IIS and then provided the complete service URL in the Xamarin application.  



GK Gowthamraj Kumar Syncfusion Team December 12, 2019 05:31 PM UTC

Hi Dinesh, 

The reported exception may occurs if the service is not reachable from the device. If you are using local host URL as web reference in the Xamarin application then this exception may occurs due to the service is not reachable. If you are using localhost URL, then the device will actually trying to connect the device itself not to your desktop/server. If you want to connect the service from desktop/server, then you to host the service in the desktop/server then you need to use the IP address of the target machine.  

Please refer the below steps for converting a URL/HTML string to PDF in Xamarin using Web Service: 
1)          Create a Web service, and include all the HTML to PDF conversion part with in the service.  
2)          Host the Web service in local machine (IIS).  
3)          Create new Xamarin project, and add the service reference of web.  
4)          Provide the input URL through the service and the PDF can be get as string. 
 

Note : Please make sure the hosted service is reachable from the device. 

Please let us know if you need any further assistance on this. 

Regards, 
Gowthamraj K 


Loader.
Up arrow icon