Oppure

Loading
03/05/21 12:27
dylan666
Ciao a tutti,
sto provando a convertire una pagina HTML "complessa" (cioè che implica l'uso di fogli di stile) in PDF e siccome il risultato di iTextSharp non mi piaceva sto provano PdfSharp con questo pacchetto: nuget.org/packages/HtmlRenderer.PdfSharp/

Una volta preso da NuGet e fatto Imports di TheArtOfDev.HtmlRenderer.PdfSharp e l'esempio per C# è questo:

class Program
{
    private static void Main(string[] args)
    {
        PdfDocument pdf = PdfGenerator.GeneratePdf("<p><h1>Hello World</h1>This is html rendered text</p>", PageSize.A4);
        pdf.Save("document.pdf");
    }
}


Che in VB.net diventa più o meno questo

        Dim pdf As PdfSharp.Pdf.PdfDocument = PdfGenerator.GeneratePdf("<p><h1>Hello World</h1>This is html rendered text</p>", PdfSharp.PageSize.A4)
        pdf.Save("document.pdf")


Ho eseguito il suddetto codice messo in un button.
Ricevo questo errore:

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=L'inizializzatore di tipo di 'TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter' ha generato un'eccezione.
  Source=HtmlRenderer.PdfSharp
  TypeName=TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter
  StackTrace:
       in TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter.get_Instance() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\Adapters\PdfSharpAdapter.cs:riga 57
       in TheArtOfDev.HtmlRenderer.PdfSharp.HtmlContainer..ctor() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\HtmlContainer.cs:riga 43
       in TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(String html, PdfGenerateConfig config, CssData cssData, EventHandler`1 stylesheetLoad, EventHandler`1 imageLoad) in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\PdfGenerator.cs:riga 110
       in TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(String html, PageSize pageSize, Int32 margin, CssData cssData, EventHandler`1 stylesheetLoad, EventHandler`1 imageLoad) in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\PdfGenerator.cs:riga 76
       in HMTL2PDF.Form1.Button3_Click(Object sender, EventArgs e) in C:\Miei Progetti\Test - HMTL2PDF\vX\HMTL2PDF\Form1.vb:riga 26
       in System.Windows.Forms.Control.OnClick(EventArgs e)
       in System.Windows.Forms.Button.OnClick(EventArgs e)
       in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       in System.Windows.Forms.Control.WndProc(Message& m)
       in System.Windows.Forms.ButtonBase.WndProc(Message& m)
       in System.Windows.Forms.Button.WndProc(Message& m)
       in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       in HMTL2PDF.My.MyApplication.Main(String[] Args) in :riga 81
       in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146233079
       Message=Obsolete and not implemted any more.
       Source=PdfSharp
       StackTrace:
            in PdfSharp.Drawing.XFontFamily.get_Families()
            in TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter..ctor() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\Adapters\PdfSharpAdapter.cs:riga 44
            in TheArtOfDev.HtmlRenderer.PdfSharp.Adapters.PdfSharpAdapter..cctor() in d:\source\github\HTML-Renderer\Source\HtmlRenderer.PdfSharp\Adapters\PdfSharpAdapter.cs:riga 33
       InnerException: 


Mi pare però molto generico...
Dove sbaglio?

Grazie in anticipo a chi risponderà
aaa
04/05/21 13:22
Thejuster
Questo problema e relativo ad una vecchia versione che hai scaricato.

L'errore avviene quando la libreria "scritta male" cerca di utilizzare un font non presente nel tuo sistema
causando quell'eccezione.


La versione che hai scaricato da quel sito è: HtmlRenderer.PdfSharp -Version 1.5.0.6

Il bug è stato risolto con la versione 1.5.1

installa questa versione con il pm.

PM> Install-Package HtmlRenderer.PdfSharp -Version 1.5.1-beta1

mire.forumfree.it/ - Mire Engine
C# UI Designer
06/05/21 12:15
dylan666
Ha funzionato perfettamente.
Era una banalità ma non ci avrei mai pensato! :)
aaa