PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 4:11 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Apr 03, 2014 3:05 pm 
Offline

Joined: Mon Mar 31, 2014 9:31 am
Posts: 4
Hi All,

It seems that the PdfDocumentRenderer.Save() does not stop others processes reading the file while it writes the file to disk, thus if you have another process which is simply moving files (File.Copy()) the PDF file can be copied regardless it is stated and it could be corrupted.

I would just like to ask for confirmation (I tend to put an exclusive lock when writing a file) and how I could work around this.

Code:
  Document doc = Documents.CreateResultDocument(data);
  //Create a renderer for PDF that uses Unicode font encoding
  PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
 //Set the MigraDoc document
  pdfRenderer.Document = doc;
  //Create and save the PDF document
   pdfRenderer.RenderDocument();
   log.Info("Saving PDF for file: " + originalFilename + " ...");
   pdfRenderer.Save(outputFilename);
   log.Info("PDF for file: " + originalFilename + " has been created => " + outputFilename);


Many thanks


Last edited by vence on Fri Apr 04, 2014 6:35 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 03, 2014 3:31 pm 
Offline
PDFsharp Guru
User avatar

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

Here's what Save(string) does internally:
Code:
Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
Save(stream);
.NET allows read access for files created this way.

Solution: create a stream with appropriate permissions and use Save(stream, true) instead.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 04, 2014 7:35 am 
Offline

Joined: Mon Mar 31, 2014 9:31 am
Posts: 4
Thanks Thomas,

Code:
  //Create stream with exclusive locks thus nobody can access the file until it has been written
   Stream stream = new FileStream(outputFilename, FileMode.Create, FileAccess.Write,FileShare.None);
   log.Info("Saving PDF for file: " + originalFilename + " ...");
   pdfRenderer.Save(stream, true);
   log.Info("PDF for file: " + originalFilename + " has been created => " + outputFilename);


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 10:12 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi, Vence,

thanks for the feedback.

@All: future versions of PDFsharp and MigraDoc will open the file internally with "FileShare.None" because it makes no sense to allow read access to a partially written PDF file.

When a constructor with fewer parameters is used, one should always check the default values of the missing parameters ... :wink:

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 369 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group