4

This works in an ASP.NET MVC application when run locally, but not when deployed on Azure:

Document doc = new Document();  
Section section = doc.AddSection();
section.AddParagraph("Some text to go into a PDF");          
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
pdfRenderer.Document = doc;
pdfRenderer.RenderDocument();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
pdfRenderer.PdfDocument.Save(stream, false);
Byte[] documentBytes = stream.ToArray();

return File(documentBytes, "application/pdf");

Locally, I get a nice PDF. On Azure, I get a blank PDF. I'm not seeing any exceptions thrown or other error messages. I found some SO answers stating that the GDI version of PDFsharp doesn't work on Azure, so I'm using the WPF version instead - same result.

I found this SO question, but I'm not clear how to apply it to an MVC app: Why is MigraDoc generating a blank pdf in my asp.net application?

Sorry if this is an obvious question, I'm just stuck!

Community
  • 1
  • 1
Robert M.
  • 575
  • 5
  • 17
  • 1
    When you say this is deployed to Azure, I'm assuming that's Azure App Service (Web Apps), if so, which tier are you running on? You may need to be on Basic or above to get this to work – Fabio Cavalcante Dec 09 '16 at 06:24
  • Is the tier related to the subscription? Right now I'm using a free trial subscription...is that my whole problem here? – Robert M. Dec 09 '16 at 12:45
  • 1
    Not related to the subscription. You can check your tier in your web app plan. Due to sandbox restrictions, this may only work on Basic and above, that is **if it works**, since GDI use is restricted and only a handful of libraries work properly with those restrictions. – Fabio Cavalcante Dec 09 '16 at 18:19
  • I'm using the WPF version of pdfsharp rather than the GDI version. – Robert M. Dec 09 '16 at 19:34
  • 1
    Do you get a complete PDF from Azure? If so then probably the problem is not with returning the stream, but with creating the PDF. I'd implement IFontResolver in the application and see if that makes a difference. – I liked the old Stack Overflow Dec 11 '16 at 08:56
  • In my understanding IFontResolver is for using private fonts, which I'm not. – Robert M. Dec 12 '16 at 01:59
  • PDFsharp must have access to the TTF files to extract information. Are the fonts you use in the %windir%\fonts folder and does your process have privileges to read them? Azure is a candidate for IFontResolver because many fonts are missing and privileges are usually not granted. – I liked the old Stack Overflow Dec 12 '16 at 07:14

1 Answers1

3

It's likely a font problem if a complete PDF arrives at the client (I asked for conformation in a comment but got no answer yet).

PDFsharp must have access to the TTF files to extract information. Are the fonts you use in the %windir%\fonts folder and does your process have privileges to read them?

Azure is a candidate for IFontResolver because many fonts are missing and privileges are usually not granted.

With IFontResolver you give PDFsharp direct access to the TTF files (as byte[]).

You can use my class EZFontResolver for that purpose:
http://developer.th-soft.com/developer/2015/12/11/ezfontresolver-a-generic-font-resolver-for-pdfsharp-and-migradoc/

I also have a sample that shows how to implement your own IFontResolver:
http://developer.th-soft.com/developer/2015/09/21/using-private-fonts-with-pdfsharp-1-50-beta-2-or-migradoc/

  • I stated *in the question* that I'm getting a complete, but blank PDF. Thanks for this info, I'll give it a try. – Robert M. Dec 12 '16 at 10:03
  • 1
    @RobertM. You wrote _blank PDF_ in the question. Since you linked to another question where the problem obviously was related to not using _Flush_ or _Seek_ I wanted to make sure the problem was not related to an incomplete transfer of the created PDF file. – I liked the old Stack Overflow Dec 12 '16 at 18:58
  • When using your EZFontResolver class, where do you put the font file? If it put in a folder in the VS solution will it get added to the path when I publish? Also, I'm not getting any kind of error message or exception right now, and there are fonts in the fonts folder... – Robert M. Dec 14 '16 at 05:39
  • This is giving me nightmares. I updated to the WPF 1.5 beta version of pdfsharp so I could use EZFontResolver (VS couldn't find IFontResolver with 1.3) and now I can't find any of the other pdfsharp classes I was using, like PdfDocumentRenderer. – Robert M. Dec 14 '16 at 06:09
  • 1
    You have to take care that fonts get deployed to Azure. Adding them to the project with compile type _Embedded Resource_ as shown in the sample is one option. Version 1.50 uses all distribution methods that version 1.32 also used and you add references like you did with earlier versions. You don't mention whether you use the source package of PDFsharp or NuGet packages. http://stackoverflow.com/q/1831794/1015447 – I liked the old Stack Overflow Dec 14 '16 at 07:25
  • My fonts are getting deployed, yes. They're in the /fonts/ folder. I think the issue with 1.50 was that I was using the nuget package, which only includes pdfsharp and not migradoc... Thanks for your patience in helping me through this. TBH I'm considering switching to iTextSharp as people report having less problems with that on Azure. – Robert M. Dec 14 '16 at 21:23