System.InvalidOperationException: ''DocumentRenderer' must be set before calling 'PrepareDocumentRenderer'.'

474 views Asked by At

I'm trying to convert html codes to pdf with pdfsharp & migradoc. I use the RenderDocument() function for Turkish characters.But after the RenderDocument() function I get this error. System.InvalidOperationException: '' DocumentRenderer 'must be set before calling' PrepareDocumentRenderer '.'

I wrote the code below by looking at the example in this link. http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

protected void btnGeneratePdf_Click(object sender, EventArgs e)
{
    string html = "";
    using (var client = new WebClient())
    {
        html = client.DownloadString("http://localhost:14670/WebForm6");
    }

    PdfGenerateConfig config = new PdfGenerateConfig();
    config.PageSize = PageSize.A4;
    config.SetMargins(20);

    var doc = PdfGenerator.GeneratePdf(html, config);

    PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
    renderer.PdfDocument = doc;
    renderer.RenderDocument();

    var tmpFile = "C://Users//mutlu.ozkurt//Desktop//Files/tmp372A.pdf";
    renderer.PdfDocument.Save(tmpFile);

    Process.Start(tmpFile);
}
1

There are 1 answers

2
I liked the old Stack Overflow On BEST ANSWER

You are using the HTML Renderer for PDF using PDFsharp that creates a PDF file, not a MigraDoc document. You mix this with sample code from MigraDoc. Things do not work this way.

Use the doc variable you get and use it to create a PDF directly without calling any MigraDoc code.