PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 3:09 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sun Dec 01, 2019 8:00 pm 
Offline

Joined: Sun Dec 01, 2019 6:25 pm
Posts: 4
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 7176 times ]
Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 01, 2019 11:07 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

Please try the WPF build of MigraDoc.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2019 9:28 am 
Offline

Joined: Sun Dec 01, 2019 6:25 pm
Posts: 4
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 7168 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2019 9:54 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2019 10:03 am 
Offline

Joined: Sun Dec 01, 2019 6:25 pm
Posts: 4
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2019 12:04 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2019 2:26 pm 
Offline

Joined: Sun Dec 01, 2019 6:25 pm
Posts: 4
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 7156 times ]
File comment: Error DispatcherObject
error.JPG
error.JPG [ 20 KiB | Viewed 7156 times ]
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 144 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group