IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

iTextSharp convertion de html vers pdf


Sujet :

C#

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut iTextSharp convertion de html vers pdf
    bonjour.

    J'ai besoin d 'un petit coup de main pour un script en c# svp. Je teste actuellement iTextSharp dans une appli winform.

    mon programme plante au niveau de la finction "HTMLWorker.ParseToList". Le message d'erreur est :
    La référence d'objet n'est pas définie à une instance d'un objet.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    using iTextSharp;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    using System.Net;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.html;
     
    namespace HtmlToPdf
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                string pathDownload = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "iTextSharp_Demo.pdf");
     
                using (FileStream newPdfFile = new FileStream(pathDownload, FileMode.Create))
                {
                    Document document = new Document(PageSize.A4);
                    StringReader htmlString = new StringReader(new WebClient().DownloadString("http://jqueryui.com/"));
     
                    List<IElement> htmlNodes = HTMLWorker.ParseToList(htmlString, null);
     
                    PdfWriter.GetInstance(document, newPdfFile);
                    document.Open();
                    foreach (IElement htmlNode in htmlNodes)
                    {
                        document.Add(htmlNode);
                    }
                    document.Close();
                }
            }
        }
    }
    que se passe t-il ? :/
    Merci.

  2. #2
    Membre émérite
    Avatar de azstar
    Homme Profil pro
    Architecte Technique BizTalk/.NET
    Inscrit en
    Juillet 2008
    Messages
    1 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Technique BizTalk/.NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 198
    Points : 2 424
    Points
    2 424
    Par défaut
    a tu initialise HTMLWorker quelque part dans ton code !!????

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    non, tout est là. Je vais essayer de faire comme tu dis !

  4. #4
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    si j'initialise HTMLWorker avant, la fonction ParseToList n'est plus reconnue !
    Le problème n'est donc pas à ce niveau. mais ce n'était pas idiot !

  5. #5
    Membre émérite
    Avatar de azstar
    Homme Profil pro
    Architecte Technique BizTalk/.NET
    Inscrit en
    Juillet 2008
    Messages
    1 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Technique BizTalk/.NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 198
    Points : 2 424
    Points
    2 424
    Par défaut
    Citation Envoyé par thor76160 Voir le message
    mon programme plante au niveau de la finction "HTMLWorker.ParseToList". Le message d'erreur est :

    Citation:
    La référence d'objet n'est pas définie à une instance d'un objet.
    ca veux dire que HTMLWorker a une valeur null

    tu peut nous passer plus de code (et merci d'nclus le DLL comme fichier joint )

  6. #6
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    c'est étrange car mon paramètre htmlString est correctement renseigné, j'ai vérifié avec le mode débug ! J'y ai bien vu du code HTML.

  7. #7
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    ça par contre ça marche !!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    using iTextSharp;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    using System.Net;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.html;
     
    namespace HtmlToPdf
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                string pathDownload = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "iTextSharp_Demo.pdf");
     
                using (FileStream newPdfFile = new FileStream(pathDownload, FileMode.Create))
                {
                    Document document = new Document(PageSize.A4);
                    //StringReader htmlString = new StringReader(new WebClient().DownloadString("http://jqueryui.com/"));
                    StringReader htmlString = new StringReader("<html><body><p>Hello World !</p></body></html>");
     
                    List<IElement> htmlNodes = HTMLWorker.ParseToList(htmlString, null);
     
                    PdfWriter.GetInstance(document, newPdfFile);
                    document.Open();
                    foreach (IElement htmlNode in htmlNodes)
                    {
                        document.Add(htmlNode);
                    }
                    document.Close();
                }
            }
        }
    }
    Le problème viendrait donc de :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new WebClient().DownloadString("http://jqueryui.com/")

  8. #8
    Membre émérite
    Avatar de azstar
    Homme Profil pro
    Architecte Technique BizTalk/.NET
    Inscrit en
    Juillet 2008
    Messages
    1 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Technique BizTalk/.NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 198
    Points : 2 424
    Points
    2 424
    Par défaut
    et c'est ce qui tu est demandé ???!!

    est seulement pour le test ?!!!

  9. #9
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    et ce qui tu est demandé ???!!
    pardon je n'ai pas compris la question

    est seulement pour le test ?!!!
    oui, je dois tester plusieurs technologies pour un futur projet, je teste donc celui-ci aujourd'hui.

  10. #10
    Membre émérite
    Avatar de azstar
    Homme Profil pro
    Architecte Technique BizTalk/.NET
    Inscrit en
    Juillet 2008
    Messages
    1 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Technique BizTalk/.NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 198
    Points : 2 424
    Points
    2 424
    Par défaut
    Alors je pense que tu doit cherche dans la côte structure des données HTML
    résultat de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new WebClient().DownloadString("http://jqueryui.com/")
    et a quel point cette méthode est compatible avec les codes html (donné d'entre )malheureusement pas toutes les sites sont standardisés par W3C

    j'espère que je n'est pas dit des bêtises

Discussions similaires

  1. Edition Html vers PDF
    Par Diablo_22 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 14/12/2007, 10h43
  2. [FPDF] Tableaux HTML vers PDF
    Par brida dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 22/10/2007, 21h06
  3. [FPDF] Convertion HTML vers PDF
    Par sami_c dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 21/09/2007, 16h43
  4. Convertion HTML vers pdf problème
    Par PrinceMaster77 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 26/07/2007, 17h25
  5. HTML vers PDF
    Par Warzak dans le forum Langage
    Réponses: 2
    Dernier message: 08/08/2006, 11h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo