2

Here is my code and it works (I used NuGet package Select.HtmlToPdf. Document.Save saves the PDF instead I would like the PDF open for user to review (instead of saving in computer)

    HtmlToPdf converter = new HtmlToPdf();

    // create a new pdf document converting an url
    PdfDocument doc = converter.ConvertUrl("www.cnn.com");

    // save pdf document
    doc.Save.(Response, false, "test.pdf");  // false: will not save the document

    // close pdf document
    doc.Close();
dotNETEngineer
  • 143
  • 2
  • 13

1 Answers1

0

To open in browser, after PDFDocument initiated:

PdfDocument doc = converter.ConvertHtmlString("www.cnn.com");
byte[] bytes = doc.Save();
string mimeType = "Application/pdf";
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.Flush(); 
Response.End();
  • Thanks for the response. I heard HtmlToPdf only works for three pages (free version). Do you suggest best one (convert the current screen without losing BootStrap CSS format) for multiple pages PDF? I tried to use iTextSharp, however I'm losing the BootStrap look and feel. – dotNETEngineer Feb 06 '18 at 14:37
  • If you want to stick to the free version, use HTMLToPDF to convert in sections of five pages (website says max is five but I’ve not tested this) and then use iTextSharp to combine pdfs (https://stackoverflow.com/questions/6029142/merging-multiple-pdfs-using-itextsharp-in-c-net) – Stewart Morgan Feb 09 '18 at 18:37
  • You’ll need to figure out what 5 pages are in terms of height and where this falls in your html output. Obviously loads of factors depending on page widh, sizes of elements, if there’s any grid views or tables to factor in. When I’ve done this in the past I was generating single/double sided pdf forms of a set , known size and then giving the option to users to save a complete pdf portfolio including all the forms – Stewart Morgan Feb 09 '18 at 18:42
  • Is there any code to refer? One of the issues is I just need to convert entire webpage with same look and feel (current display - not URL) to PDF. Any other tools to use? I may have more than 5 pages. – dotNETEngineer Feb 10 '18 at 17:44