Create PDF from Template

11,263

You can use PdfSharp plugin via NuGet. Just create html template file, read it as string, replace what you need, and pass full html to function bellow.

using TheArtOfDev.HtmlRenderer.PdfSharp;

    public static bool PdfSharpConvert(String html, string SaveTo)
    {
        try
        {
            using (var pdf = PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4))
            {
                pdf.Save(SaveTo);
            }

            return true;
        }
        catch (Exception er)
        {
            return false;
        }
        finally
        {
            GC.Collect();
        }
    }
Share:
11,263
Weedalf
Author by

Weedalf

Updated on June 04, 2022

Comments

  • Weedalf
    Weedalf almost 2 years

    I need to create a PDF File with c#. It have to be that I need a template (eg. Editable PDF or HTML Template). In this template I have to insert text and Picutures.

    I have no ideas how to do this. Could someone say me a technique I could use do this?

    • ProgrammingLlama
      ProgrammingLlama over 5 years
      I've removed your request for libraries since that's off-topic.
    • anhtv13
      anhtv13 over 5 years
      try itextsharp to generate pdf from html template
    • Jongware
      Jongware over 5 years
      @John: and so it's now a request for a full tutorial, which is similarly off-topic -- in the same sentence, no less: "4. Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic ..."
    • ProgrammingLlama
      ProgrammingLlama over 5 years
      @usr2564301 That's true.
    • Weedalf
      Weedalf over 5 years
      i dont want a tutorial. just an idea -.-
    • Ryan
      Ryan over 5 years
      Why do you need to create a PDF? Why not just use HTML forms, or something else?
  • Steffen Bauer
    Steffen Bauer about 3 years
    How can you deal with page related things like page breaks with an approach like this?
  • Tommix
    Tommix about 3 years
    Sorry dont know, in my experience better to write pdf in code not from html. Like other answer - choosen that path. Static template but at least im in control of every aspect. This example which i provided have it's shortcomings, like page breaks. So i guess this answer is good until you have 1 page.