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.

Mon Oct 28, 2013 2:30 pm

I am evaluating using your HTML-to-PDF conversion component for my company's HL7 server reports. I am currently evaluating the free version.

My problem is how to convert from a string containing the HTML to a string containing the PDF (string-to-string). I have loaded the HTML into a memory stream, and using the SaveToStream() method to capture the PDF as a stream. Getting strings to/from streams is the easy part, the problem I'm having is that the component is always throwing an exception. I typically get "Unknown/Invalid/Unsupported format", this is for a simplistic HTML string:

"<!DOCTYPE html><html><body><h1>Header 1</h1><p>First paragraph</p></body></html>";

below is a snippet of the code (just test code) calling the component:

try
{
PdfDocument pdfdoc = new PdfDocument();

byte[] htmlBytes = Encoding.ASCII.GetBytes(html);

pdfdoc.PageSettings.Orientation = PdfPageOrientation.Portrait;

pdfdoc.LoadFromBytes(htmlBytes, string.Empty);

MemoryStream pdfstream = new MemoryStream();
pdfdoc.SaveToStream(pdfstream);
byte[] pdfbytes = pdfstream.ToArray();

pdf = System.Convert.ToBase64String(pdfbytes);
error = "success";
return;
}
catch (Exception e)
{
error = e.Message;
return;
}

bp100a
 
Posts: 2
Joined: Fri Oct 25, 2013 8:21 pm

Tue Oct 29, 2013 5:27 am

Hello,

Thanks for your inquiry.
The pdfdoc.LoadFromBytes() method can only load the byte array with the pdf file content, and we recommend you use Spire.Doc to convert HTML string to PDF string. Please refer to the following method, if you need to use Spire.Pdf, you could evaluate our Spire.Office product.
Code: Select all
 
string pdfstring = null;
string htmlstring= "<!DOCTYPE html><html><body><h1>Header 1</h1><p>First paragraph</p></body></html>";
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
para.AppendHTML(htmlstring);
using (MemoryStream pdfstream = new MemoryStream())
     {
       doc.SaveToStream(pdfstream, Spire.Doc.FileFormat.PDF);
       byte[] pdfbytes = pdfstream.ToArray();
       pdfstring = System.Convert.ToBase64String(pdfbytes);     
     }

If there are any questions, welcome to get it back to us.

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Tue Oct 29, 2013 8:19 pm

Thank you. I tried what you suggested and I get the following:

"The 'html' element is not declared".

My code (just to show you exactly what I did)

Using Spire.Doc;
Using Spire.Doc.Documents;
o
o
o
string htmlstring = "<!DOCTYPE html><html><body><h1>Header 1</h1><p>First paragraph</p></body></html>";
try
{
Spire.Doc.Document doc = new Spire.Doc.Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
para.AppendHTML(htmlstring);

using (MemoryStream pdfstream = new MemoryStream())
{
doc.SaveToStream(pdfstream, Spire.Doc.FileFormat.PDF);
byte[] pdfbytes = pdfstream.ToArray();
pdf = System.Convert.ToBase64String(pdfbytes);
status = "success";
return;
}
}
catch (Exception e)
{
status = e.Message;
return;
}

bp100a
 
Posts: 2
Joined: Fri Oct 25, 2013 8:21 pm

Wed Oct 30, 2013 2:30 am

Hello,
Thanks for your quick reply.
We have tested it with Spire.Doc4.9.32, it works fine and there is no error you experienced. I have attached the project we test, The project does not include the spire.doc references(please download Spire.Doc4.9.32 and add it).
There is another solution (only need Spire.Pdf) you can try that we have a new plugin for html conversion. You could get it from http://e-iceblue.com/downloads/HTMLConv ... .2.3.3.zip, and a version for x64 from http://e-iceblue.com/downloads/HTMLConv ... .3-x64.zip. Unzip the convertor plugin package and copy the folder plugins into the bin folder, we suppose that the bin folder contains the file Spire.Pdf.dll in runtime. I attached a simple code. Calling the plugins is very simple:
Code: Select all
using System.Drawing;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter.Qt;

namespace SPIREPDF_89
{
    class Program
    {
        static void Main(string[] args)
        {
            String fileName = Path.GetTempFileName() + ".html";
            String pdfFile = Path.GetTempFileName();
            string htmlstring = "<!DOCTYPE html><html><body><h1>Header 1</h1><p>First paragraph</p></body></html>";
            File.WriteAllText(fileName, htmlstring);

            HtmlConverter.Convert(fileName, pdfFile,

                //enable javascript
                true,

                //load timeout
                100 * 1000,

                //page size
                new SizeF(612, 792),

                //page margins
                new PdfMargins(0, 0));
            string pdf = System.Convert.ToBase64String(File.ReadAllBytes(pdfFile));
        }
    }
}

You could get a full demo from http://www.e-iceblue.com/downloads/temp/SPIREPDF-89.zip.

If you have any question, please tell us.

Regards,
Gary
e-iceblue support team.
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Nov 04, 2013 9:16 am

Hello,

Have you tested the new solution? Has your issue been resolved? Could you please give us some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks And Regards,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.PDF