Wkhtmltopdf unable to generate PDF .Net Core Api - c#

I have tried to generate PDF from HTML template using wkhtmltopdf on .NET Core 5 web API, but it keeps returning the error
“wkhtmltopdf” cannot be opened because the developer cannot be verified.
System.Exception
at Wkhtmltopdf.NetCore.WkhtmlDriver.Convert(String wkhtmlPath, String switches, String html)
at Wkhtmltopdf.NetCore.GeneratePdf.GetPDF(String html)
I downloaded the Rotativa folder that contains .dll, I also set the properties to Always for Copy.
Here is my code
var HTML =#"<!DOCTYPE HTML> <HTML><h2></h2></HTML>"
var pdf = _generatePdf.GetPDF("html");
var pdfStream = new System.IO.MemoryStream();
return new FileStreamResult(pdfStream, "application/pdf");

Related

SpirePDF MAUI PdfDocument Display

I have been working on getting a PDF Viewer working in Net MAUI. In Xamarin, I displayed a PDF in Webview. Not a problem. In MAUI, you don't do that. I installed SpirePDF(free) and have it loading the PDF from file. When adding it to VerticalStackLayout (which will be the Content for a ScrollView), it fails on conversion to IView. Any ideas or suggestions?
VerticalStackLayout vsl = new VerticalStackLayout(); PdfDocument viewPdf = new PdfDocument(); Assembly.GetExecutingAssembly().GetType().GetTypeInfo().Assembly.GetManifestResourceStream("CommanderGrady.Resources.Images.adventure218.pdf"); viewPdf.LoadFromFile(#"C:\Users\leuol\source\repos\CommanderGrady\CommanderGrady\Resources\Images\adventure218.pdf"); vsl.Add((IView)viewPdf); return vsl; }
I have done a sample and the pdf can show in the browser. So you can try the following code.
string fullpath = xxx; // provide the file path
await Launcher.OpenAsync(new OpenFileRequest
{
File = new ReadOnlyFile(fullpath)
});

Google Drive API / GUI exports invalid PDF - not readable by iText7

I'm trying to export multiple Google Docs files via Google Drive API into Pdf and merge them into one using iText7 but it throws exception iText.IO.Exceptions.IOException: 'PDF header not found.' because of the weird PDF format from Google export.
Google Disk generated PDF content (read with notepad) is not valid PDF.
File content starts like this 倥䙄ㄭ㐮┊ㄊ instead of something like %PDF-1.4
The uploaded PDF file is readable from Google Disk without any problem and it is readable even if I export the Stream directly to the disk. File content is exactly the same when I download file manually through Google Docs GUI.
Here is my code to export files via API:
var mimeType = "application/pdf";
var file = GetFile(sourceFile);
var pdfRequest = _driveService.Files.Export(sourceFile, mimeType);
var stream = pdfRequest.ExecuteAsStream();
Then I'm uploading PDF back into Google Drive via it's API
var newFile = new Google.Apis.Drive.v3.Data.File();
newFile.MimeType = mimeType;
newFile.Parents = new List<string>() { targetFolder };
var createRequest = _driveService.Files.Create(newFile, stream, mimeType);
createRequest.SupportsAllDrives = true;
var createResult = createRequest.Upload();
Weirdly enough the format of exported PDF is ok when I use
var text = pdfRequest.Execute(); instead of pdfRequest.ExecuteAsStream (it starts with %PDF-1.7).
But Execute() returns string instead of Stream.
Is there any way to get standard PDF format from Google Disk API or convert it in any possible way?
The problem was in the iText7 itself. It considered PDF as invalid but it probably just does not support PDFs in iso8859_2 encoding.
I tried to use PDFSharp instead and everything went smoothly.
I've used ExecuteAsStream() from Google Disk API to get PDF Stream with no problems at all so it wasnt at fault.
Thanks for all your tips.

Generating Word Document in Aspnet Core

I am migrating an application from asp.net MVC to Aspnet Core v2.1.
I have the following code to generate a Word Document and when I hover over the document I can see the properties such as Paragraphs, Images, Footer etc all set; but when I save I always get a 5Kb document with no content. It should return the document as a stream to download by the browser, but I have also included an explicit .SaveAs("C:\blah") which produces same file.
var fileStream = new MemoryStream();
DocX document = DocX.Create(fileStream, DocumentTypes.Document);
Paragraph p1 = document.InsertParagraph();
p1.Append("Hello world");
document.SaveAs(fileStream);
fileStream.Position = 0;
fileStream.Seek(0, SeekOrigin.Begin);
document.SaveAs("C:\\Code\\MyFile.docx");
var fsr = new FileStreamResult(fileStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
fsr.FileDownloadName = "MyFile.docx";
return fsr;
I don't get any errors either in the application or in my Event Viewer.
Any ideas?
Looks like the DocX library by Xceed is not yet supporting .Net Core. DocX is not supported in .net core applications.
You can try using EPPlus instead.

iText7 html2Pdf convert error - specified method is not supported

I'm trying to convert HTML file to PDF with iText7 add-on html2Pdf. I have this code and always when I call method ConvertToPdf program failed with error: specified method is not supported.
I'm trying it with valid trial licence key. I also try it without ConverterProperties and the result was the same.
Is there another way how to create PDF from HTML file with iText ?
ConverterProperties props = new ConverterProperties();
props.SetMediaDeviceDescription(new MediaDeviceDescription(MediaType.ALL));
LicenseKey.LoadLicenseFile(#"C:\Users\blah\kerz.xml");
string ppa = #"C:\Users\blah\Desktop\html\index.html";
HtmlConverter.ConvertToPdf(new FileInfo(ppa), new FileInfo(#"C:\Users\blah\Desktop\html\a.pdf"),props);

C# Winnovative HTML to PDF

I am searching for a solution to convert HTML to PDF with external CSS support. I downloaded the trial version of the Winnovative Toolkit Total v11.14, and tried out the demo application for the method public byte[] GetPdfBytesFromHtmlString (string htmlString, string urlBase). The PDF files are generated, but the CSS is not applied.
Note: I tried the same input HTML string and base URL in the demo site. It's working fine, so I don't know why it's not working in my system. The demo application is shared in v11.14 ZIP files.
Input provided for this method:
htmlString = HTML source of the url 'http://www.winnovative-software.com/'
urlBase = "http://www.winnovative-software.com/"
Are you using any proxy to access Internet? In this case you should set the HtmlToPdfConverter.ProxyOptions object properties in your code.

Categories

Resources