PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Jan 17, 2018 12:09 pm 
Offline

Joined: Wed Jan 17, 2018 11:53 am
Posts: 3
Hello

I read in the FAQ that "Beginning with version 1.50, PDFsharp/MigraDoc should work under Medium Trust". So I added PDFsharp and MigraDoc to my ASP.NET MVC project using the NuGet package PDFsharp-MigraDoc-gdi version 1.50.4740-beta5, tested that I could successfully create a PDF document in debug mode, and then ran it in release mode setting trust level="Medium" in web.config. Then the first reference to the MigraDoc.DocumentObjectModel namespace resulted in an error "System.Security.SecurityException: That assembly does not allow partially trusted callers.". I understand that Medium Trust prevents access to the fonts installed with the operating system and that I may have to use a font resolver to use fonts, but the exception occurs before attempting to add any content to the document, so it looks like the assembly itself cannot be run in Medium Trust. I have also tried with the Core package (PDFsharp-MigraDoc version 1.50.4740-beta5) and had the same result.

I would be grateful if anybody could tell me what I need to do in order to actually be able to use PDFsharp/MigraDoc in a Medium Trust environment. Many thanks in advance.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 17, 2018 5:11 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

Do you really need Medium Trust?
PDFsharp/MigraDoc works on Azure Servers.

I never tried Medium Trust, but you can download the source code, compile PDFsharp and MigraDoc for Medium Trust and see if any code changes are needed.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 17, 2018 6:25 pm 
Offline

Joined: Wed Jan 17, 2018 11:53 am
Posts: 3
Hi

Thanks for your reply. Yes I am afraid I do need Medium Trust because I have to deploy the solution to a hosted web site in a shared hosting environment and our web hosting provider runs such web sites under Medium Trust.

But may I ask why the FAQ says "Beginning with version 1.50, PDFsharp/MigraDoc should work under Medium Trust" if this has never been tried?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 18, 2018 9:01 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
DClairet wrote:
But may I ask why the FAQ says "Beginning with version 1.50, PDFsharp/MigraDoc should work under Medium Trust" if this has never been tried?
It says "should" because it was not tested.
The person who changed the implementation told me that all method calls that are known to make problems with Medium Trust had been removed.
They also told me that Medium Trust no longer really mattered because Azure used a different approach.

Medium Trust is not my area of expertise. Maybe it is just a matter of project configuration and you can get it working without making code changes.
If you are able, ready, and willing to investigate which changes are needed to get Medium Trust going, your feedback will be welcome.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 18, 2018 12:08 pm 
Offline

Joined: Wed Jan 17, 2018 11:53 am
Posts: 3
OK, I have downloaded the source code (PDFsharp-MigraDocFoundation-1_50-beta5) from SourceForge and made some changes to get it to work in Medium trust.

First I added the [assembly: AllowPartiallyTrustedCallers] attribute to AssemblyInfo.cs in all assemblies (other than the *-gdi and *-wpf assemblies, which I have not used). I presume that the attribute is needed because those assemblies target the .NET Framework 2.0; it might not be needed if they were targeting a more recent version of the Framework.

Then I re-built the assemblies, referenced the re-built assemblies in my application, and ran the application in Medium trust. The original "That assembly does not allow partially trusted callers" was gone but I got a problem when the application attempted to add an image to the PDF document:

var image = section.Headers.Primary.AddImage(imageFilePath);

This caused the following exception:

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Directory.InternalGetCurrentDirectory(Boolean checkHost)
at MigraDoc.DocumentObjectModel.Shapes.Image.GetFilePath(String workingDir)
at MigraDoc.Rendering.ImageRenderer.Format(Area area, FormatInfo previousFormatInfo)
(...)

The GetFilePath method of the Image class makes a call to Directory.GetCurrentDirectory(), which is not supported in Medium trust.

As my application passes the full, absolute path to the image, I thought that there was no need for the Image.GetFilePath method to try and build the path, so I added the following code to that method (immediately below if (Name.StartsWith("base64:")) return Name;):

if (Path.IsPathRooted(Name))
return Name;

Note that Path.IsPathRooted returns true if the path is an absolute path but may also return true in some other cases (e.g. if the path starts with a '/' character). So this may need to be refined. However, this was good enough for my purposes.

Having done that, I then got into the expected problem with fonts:

System.Security.SecurityException: Request failed.
at PdfSharp.Drawing.XFontSource.ReadFontBytesFromGdi(Font gdiFont)
at PdfSharp.Fonts.PlatformFontResolver.CreateFontSource(String familyName, FontResolvingOptions fontResolvingOptions, Font& font, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Fonts.FontFactory.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
(...)

So I implemented a custom IFontResolver implementation, as suggested in the FAQ and following the example in http://developer.th-soft.com/developer/ ... -migradoc/ , to load font data from private font files included as part of the web application.

I was then able to successfully create a PDF document when running in Medium trust, both on my local machine and on the web server of our web hosting provider.

Note however that my application, at this stage, does not do much with PDFsharp/MigraDoc (it just contains code to create one particular PDF document, quite similar to the "Invoice" sample provided as part of the MigraDoc samples in the PDFsharp web site). It is possible that other, more sophisticated applications could encounter other problems in Medium trust).


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 139 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