NReco HTML-to-PDF Generator GeneratePdfFromFiles method throws exception

2k views Asked by At

I have a fully working system for creating single page PDFs from HTML as below;

After initializing the converter

var nRecoHTMLToPDFConverter = new HtmlToPdfConverter();
nRecoHTMLToPDFConverter = PDFGenerator.PDFSettings(nRecoHTMLToPDFConverter);
string PDFContents;

PDFContents is an HTML string which is being populated.

The following command works perfectly and gives me the byte[] which I can return;

createDTO.PDFContent = nRecoHTMLToPDFConverter.GeneratePdf(PDFContents);

The problem arises when I want to test and develop the multi page functionality of the NReco library and change an arbitrary number of HTML pages to PDF pages.

var stringArray = new string[]
{
   PDFContents, PDFContents,
};
var stream = new MemoryStream();
nRecoHTMLToPDFConverter.GeneratePdfFromFiles(stringArray, null, stream);
var mybyteArray = stream.ToArray();

the PDFContents are exactly the same as above. On paper, this should give me the byte array for 2 identical PDF pages however on call to GeneratePdfFromFiles method, I get the following exception;

WkHtmlToPdfException: Exit with code 1 due to network error: HostNotFoundError (exit code: 1)

Please help me resolve this if you have experience with this library and its complexities. I have a feeling that I'm not familiar with the proper use of a Stream object in this scenario. I've tested the working single page line and the malfunctioning multi page lines on the same method call so their context would be identical.

Many thanks

1

There are 1 answers

4
Vitaliy Fedorchenko On

GeneratePdfFromFiles method you used expects array of file names (or URLs): https://www.nrecosite.com/doc/NReco.PdfGenerator/?topic=html/M_NReco_PdfGenerator_HtmlToPdfConverter_GeneratePdfFromFiles_1.htm

If you operate with HTML content as .NET strings you may simply save it to temp files, generate PDF and remove after that.