PDFsharp generates blank page in Azure, but works locally - c#

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!

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/

Related

.net Maui android how to retrieve file from external fileserver

I am making an app that allows you to open and edit a pdf file on tablets. Because i usually work with .NET, i decided to write it in .NET MAUI. That way i also have access to windows tablets.
It uses Itext as its main library to read and edit the pdf's.
I have an external shared fileserver that anyone can access when they are coneected to the WIFI.
I'd like to access that fileserver when i connect from my android tablet using Itext pdfreader.
How do I achieve this correctly?
Am i missing a library or a package which would allow to me to access that file?
Are there options i haven't discovered yet?
This works on windows tablets:
string dest "\\\\Path\\to\\File\\";
string file = "\\\\Path\\to\\File\\file.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfReader(file), new PdfWriter(dest));
I have tried :
string file = Environment.GetFolderPath(Environment.SpecialFolder.Windows)+ "\\Path\to\File\file.pdf";
string file = "\\\\Path\\to\\File\\file.pdf";
All of them result in file not found
Among the getfolderpath options ive tried a dozen, none of them seem to work.
thank you for your time
So i ended up solving this by transforming the document into a base64 string and sending it through an api that i had.
i used the classic httprequest aproach that you can look up and copy anywhere.

How to upload to OneDrive using Microsoft Graph Api in c#

I have been trying to upload to a OneDrive account and I am hopelessly stuck not being able to upload neither less or greater than 4MB files. I have no issues accessing the drive at all, since I have working functions that create a folder, rename files/folders, and a delete files/folders.
https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=csharp
This documentation on Microsoft Graph API is very friendly to HTTP code, and I believe I am able to fairly "translate" the documentation to C#, but still fail to grab a file and upload to OneDrive. Some places online seem to be using byte arrays? Which I am completely unfamiliar with since my primary language is C++ and we just use ifstream/ofstream. Anyways, here is the portion of code in specific (I hope this is enough):
var item = await _client.Users[userID].Drive.Items[FolderID]//"01YZM7SMVOQ7YVNBXPZFFKNQAU5OB3XA3K"].Content
.ItemWithPath("LessThan4MB.txt")//"D:\\LessThan4MB.txt")
.CreateUploadSession()
.Request()
.PostAsync();
Console.WriteLine("done printing");
As it stands, it uploads a temporary file that has a tilde "~" in the OneDrive (like as if I was only able to open but not import any data from the file onto it). If I swap the name of the file so it includes the file location it throws an error:
Message: Found a function 'microsoft.graph.createUploadSession' on an open property. Functions on open properties are not supported.
Try this approach with memory stream and PutAsync<DriveItem> request:
string path = "D:\\LessThan4MB.txt";
byte[] data = System.IO.File.ReadAllBytes(path);
using (Stream stream = new MemoryStream(data))
{
var item = await _client.Me.Drive.Items[FolderID]
.ItemWithPath("LessThan4MB.txt")
.Content
.Request()
.PutAsync<DriveItem>(stream);
}
I am assuming you have already granted Microsoft Graph Files.ReadWrite.All permission. Check your API permission. I tested this code snippet with pretty old Microsoft.Graph library version 1.21.0. Hopefully it will work for you too.

How to read data from CloudFile (Azure File Storage)?

Have a problem to read data from CloudFile. I'm using Azure File Storage, .net core 2.1.
Try to read data from the file that was uploaded to File storage.
Get a reference to file, but don't understand what to do next. Try to use different solutions, but they not work
cloudFile = directoryReference.GetFileReference(fileName);
Can you please help how to work with it to read all data(text, etc.)?
Will have formats as .doc, .pdf, etc.
Thanks!
You could download the text from your doc (this is from the example in the link below):
string text = cloudFile.DownloadTextAsync().Result;
Take a look at this. It explains how to use the storage API specifically the file storage API
https://learn.microsoft.com/en-us/azure/storage/files/storage-dotnet-how-to-use-files

Read an Excel (xlsx) file on OneDrive using C# & ASP.NET MVC

I have a OneDrive shared link for an excel file with some data. And I need to read this excel file through my C# ASP.NET MVC web application without downloading it to local PC. But I'm new to this 'OneDrive API'. So I have a serious challenge with this. I'm familiar with local file read as in,
EX:
var package = new ExcelPackage(new System.IO.FileInfo(PATH));
I'd really appreciate if anyone can guide me or provide a sample code to do this. I just need to access the ExcelPackage of the .xlsx file. Thank you in advance.
PS : I have tried some options like 'WebRequest', 'Webclient', etc. :(
You could try something like HttpClient.GetStreamAsync to open a download stream for the remote file and then pass the returned Stream to the ExcelPackage constructor (documentation states it has a constructor that accepts an instance of a Stream).
Hope it helps!

Cannot open pdf created by ITextSharp

I am using ITextSharp for creating pdf in ASP.net, every thing works fine on my local machine, when I run it on IIS server, pdf is created successfully and can be open and view in the folder it is made, but I cannot open the pdf programmatically from C#.
I am using Process.Start(path) to open the file.
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("TransferLetter/" + filenamee), FileMode.Create));
doc.Open();
DateTime date = DateTime.Now.Date;
html = html.Replace("[Date]", Request["Date"] + "");
html = html.Replace("[Address]", Request["MailingAddress"].ToString());
html = html.Replace("[PlotNo]", Request["PlotNumber"].ToString());
html = html.Replace("[Block]", Request["Block"].ToString());
html = html.Replace("[Size]", Request["PlotSize"].ToString());
string pa = Server.MapPath("TransferLetter/" + filenamee);
System.Diagnostics.Process.Start(pa);
This will never work this way. System.Diagnostics.Process.Start() runs on the server, not the client. When you're developing on your local machine you are both the server and the client so it appears to work but once you separate these two you get your failure.
To say that again but in a different way, you are asking the server to build your PDF and then you are instructing the server to launch its local copy of Adobe Reader and display the PDF to whoever is physically logged into the server at the moment. (Ok, not 100% true but pretty close.)
Instead you need to send the PDF to the client using something like Response.Redirect() or Response.Write(). Looking at your code you should be able to perform:
Response.Redirect("TransferLetter/" + filenamee)
Using iTextSharp you don't actually have to even write the PDF to disk. You could use a MemoryStream instead of a FileStream, call MemoryStream.ToArray() write before disposing of it and using Response.Write() on that byte array. If you are writing small PDFs and/or are planning on multiple people accessing this that might be the safer way.
you need to use ~ tilde operator to specify the current project folder path
Try This:
string pa = Server.MapPath("~/TransferLetter/" + filenamee);

Categories