PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

RenderDocument() error in Azure
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4069
Page 1 of 1

Author:  Davy [ Sun Dec 01, 2019 8:00 pm ]
Post subject:  RenderDocument() error in Azure

I have a problem when I try to generate a pdf file in Azure. When I test the code localy everything works fine. But when I deploy my server onto Azure I get the error that you can see in the image. The error occurs when the RenderDocument method is being called. I don't know what the problem is. Am I doing anything wrong?

I use the NuGet package PDFsharp-MigraDoc-gdi (1.50.5147).

The code I'm using is:
using test_api.Model;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using PdfSharp.Pdf;
using System.IO;

namespace test_api.Services
{
public class PDFGenerator
{
private Document document;
private TextFrame generalFrame;
private Table table;
PdfDocumentRenderer renderer;

private Color TableBorder = Colors.Black;
private Color TableGray = Colors.LightGray;
private Factuur factuur;
MemoryStream stream;

public void GeneratePDF(Factuur factuur)
{
this.factuur = factuur;
// Create a new MigraDoc document
CreateDocument();
DefineStyles();
CreatePage();
FillContent();
this.renderer = new PdfDocumentRenderer();
renderer.Document = document;
renderer.RenderDocument();
stream = new MemoryStream();
renderer.PdfDocument.Save(stream,false);
}
public MemoryStream GetStream()
{
return stream;
}
private void CreateDocument()
{
this.document = new Document();
this.document.Info.Title = "test1";
this.document.Info.Subject = "test2";
this.document.Info.Author = "test3";
}
private void DefineStyles()
{
//Normal style
Style style = this.document.Styles["Normal"];
style.Font.Name = "Verdana";

style = this.document.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);

style = this.document.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);

//Create new style called Table
style = this.document.Styles.AddStyle("Table", "Normal");
style.Font.Name = "Verdana";
style.Font.Name = "Times New Roman";
style.Font.Size = 9;

//Create new style called Titel
style = this.document.Styles.AddStyle("Titel", "Normal");
style.ParagraphFormat.SpaceBefore = "5mm";
style.ParagraphFormat.SpaceAfter = "5mm";
style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
style.Font.Size = 22;

//Create new style called Reference
style = this.document.Styles.AddStyle("Reference", "Normal");
style.ParagraphFormat.SpaceBefore = "5mm";
style.ParagraphFormat.SpaceAfter = "5mm";
style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
}
private void CreatePage()
{
//Each MigraDoc document needs at least one section.
Section section = this.document.AddSection();

// Create footer
Paragraph paragraph = section.Footers.Primary.AddParagraph();
paragraph.AddText("test");
paragraph.Format.Font.Size = 9;
paragraph.Format.Alignment = ParagraphAlignment.Center;

// Create the text frame for the general information
this.generalFrame = section.AddTextFrame();
this.generalFrame.Height = "3.0cm";
this.generalFrame.Width = "5.0cm";
this.generalFrame.Left = ShapePosition.Right;
this.generalFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.generalFrame.Top = "1.75cm";
this.generalFrame.RelativeVertical = RelativeVertical.Page;

// Add the title of the restaurant
paragraph = section.AddParagraph();
paragraph.Format.SpaceBefore = "1.25cm";
paragraph.Style = "Titel";
paragraph.AddFormattedText(factuur.Restaurant.Naam, TextFormat.Bold);

// Add the title on top of the table
paragraph = section.AddParagraph();
paragraph.Format.SpaceBefore = "0.5cm";
paragraph.Style = "Reference";
paragraph.AddFormattedText("Bestellingen", TextFormat.Bold);

// Create the bestelling table
this.table = section.AddTable();
this.table.Style = "Table";
this.table.Borders.Color = TableBorder;
this.table.Borders.Width = 0.25;
this.table.Borders.Left.Width = 0.5;
this.table.Borders.Right.Width = 0.5;
this.table.Rows.LeftIndent = 0;

// Before you can add a row, you must define the columns
Column column = this.table.AddColumn("7cm");
column.Format.Alignment = ParagraphAlignment.Left;

Column column1 = this.table.AddColumn("1.5cm");
column1.Format.Alignment = ParagraphAlignment.Left;

Column column2 = this.table.AddColumn("2.5cm");
column2.Format.Alignment = ParagraphAlignment.Left;

Column column3 = this.table.AddColumn("1.5cm");
column3.Format.Alignment = ParagraphAlignment.Left;

Column column4 = this.table.AddColumn("2.5cm");
column4.Format.Alignment = ParagraphAlignment.Left;

// Create head of table
Row row = table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = false;
row.Shading.Color = TableGray;
row.TopPadding = 3;
row.BottomPadding = 3;

row.Cells[0].AddParagraph("1");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;

row.Cells[1].AddParagraph("2");
row.Cells[1].Format.Font.Bold = true;
row.Cells[1].Format.Alignment = ParagraphAlignment.Left;
row.Cells[1].VerticalAlignment = VerticalAlignment.Center;

row.Cells[2].AddParagraph("3");
row.Cells[2].Format.Font.Bold = true;
row.Cells[2].Format.Alignment = ParagraphAlignment.Left;
row.Cells[2].VerticalAlignment = VerticalAlignment.Center;

row.Cells[3].AddParagraph("4");
row.Cells[3].Format.Font.Bold = true;
row.Cells[3].Format.Alignment = ParagraphAlignment.Left;
row.Cells[3].VerticalAlignment = VerticalAlignment.Center;

row.Cells[4].AddParagraph("5");
row.Cells[4].Format.Font.Bold = true;
row.Cells[4].Format.Alignment = ParagraphAlignment.Left;
row.Cells[4].VerticalAlignment = VerticalAlignment.Center;

}

private void FillContent()
{
// Fill general info in the general info text frame
Paragraph paragraph = this.generalFrame.AddParagraph();
//paragraph.AddText("Naam: " + "Iemand");
//paragraph.AddLineBreak();
paragraph.AddText("Factuurdatum: ");
paragraph.AddDateField("dd.MM.yyyy");
paragraph.AddLineBreak();
paragraph.AddText("Factuurnummer: " + factuur.Id);

// Iterate the invoice items
foreach (Product product in factuur.Producten)
{
Row row1 = this.table.AddRow();
row1.TopPadding = 3;
row1.Cells[0].AddParagraph(product.Naam);
row1.Cells[1].AddParagraph(product.Aantal.ToString());
row1.Cells[2].AddParagraph("€ " + product.Prijs.ToString("0.00"));
row1.Cells[3].AddParagraph("21%");
row1.Cells[4].AddParagraph("€ " + (product.Aantal*product.Prijs).ToString("0.00"));
row1.BottomPadding = 3;

}

// Add an invisible row as a space line to the table
Row row = this.table.AddRow();
row.Borders.Visible = false;


// Add basisbedrag row
double btw = factuur.TotaalPrijs / 121 * 21;
row = this.table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("Basisbedrag:");
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 3;
row.Cells[4].AddParagraph("€ " + (factuur.TotaalPrijs - btw).ToString("0.00"));
row.TopPadding = 3;
row.BottomPadding = 3;

// Add btwbedag row
row = this.table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("21% btw:");
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 3;
row.Cells[4].AddParagraph("€ " + btw.ToString("0.00"));
row.TopPadding = 3;
row.BottomPadding = 3;

// Add the total price row
row = this.table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("Totaalbedrag:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 3;
row.Cells[4].AddParagraph("€ " + factuur.TotaalPrijs.ToString("0.00"));
row.Cells[4].Format.Font.Bold = true;
row.TopPadding = 3;
row.BottomPadding = 3;

}
}
}

Attachments:
File comment: Error from Azure
error.png
error.png [ 50.21 KiB | Viewed 7251 times ]

Author:  TH-Soft [ Sun Dec 01, 2019 11:07 pm ]
Post subject:  Re: RenderDocument() error in Azure

Hi!

Please try the WPF build of MigraDoc.

Author:  Davy [ Mon Dec 02, 2019 9:28 am ]
Post subject:  Re: RenderDocument() error in Azure

When I try to use the PDFsharp-MigraDoc-wpf (1.50.5147) I get the error from the image below. Am I assining the document wrong?

Attachments:
File comment: Error when using wpf
error.JPG
error.JPG [ 28.75 KiB | Viewed 7243 times ]

Author:  Thomas Hoevel [ Mon Dec 02, 2019 9:54 am ]
Post subject:  Re: RenderDocument() error in Azure

Davy wrote:
Am I assining the document wrong?
No, there is a deployment problem and not all assemblies required by MigraDoc/PDFsharp were published to the server.

Author:  Davy [ Mon Dec 02, 2019 10:03 am ]
Post subject:  Re: RenderDocument() error in Azure

But the error I get now I get when I try running it local. Is there a way to fix this error or do I need to go back to gdi and fix the other error somehow?

Author:  Thomas Hoevel [ Mon Dec 02, 2019 12:04 pm ]
Post subject:  Re: RenderDocument() error in Azure

Davy wrote:
Is there a way to fix this error or do I need to go back to gdi and fix the other error somehow?
Fix this error by adding a reference to PresentationCore to your project.

Author:  Davy [ Mon Dec 02, 2019 2:26 pm ]
Post subject:  Re: RenderDocument() error in Azure

I have add a reference to the Presentationcore. Now I get the error shown in the picture. Already addded a reference to the WindowsBase. I'm using asp.net core 2.2, I think it's just not possible to use the DispatcherObject in .net core or am I wrong?

Attachments:
File comment: references
references.JPG
references.JPG [ 29.68 KiB | Viewed 7231 times ]
File comment: Error DispatcherObject
error.JPG
error.JPG [ 20 KiB | Viewed 7231 times ]

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/