tuespetre / TuesPechkin

A .NET wrapper for the wkhtmltopdf library with an object-oriented API.
326 stars 116 forks source link

HeaderSettings HtmlUrl gives a junk character. not showing the header and main content #133

Open renjithrainy opened 8 years ago

renjithrainy commented 8 years ago

Hello I am using html url to populate the header part of the pdf. But I see that the header and main content are not coming but instead a junk ascii character is shown at the top. Below is my code. Could you please tell me what is wrong in this? thanks in advance

string headerPath = Path.Combine(Path.GetTempPath(), String.Format("{0}.html", Guid.NewGuid()));
            System.IO.File.WriteAllText(headerPath, "<head>my  outage header</head>");
            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = true,
                    DocumentTitle = "Test Report",
                    PaperSize = PaperKind.A4,
                    OutputFormat = TuesPechkin.GlobalSettings.DocumentOutputFormat.PDF,
                    Margins =
                    {
                        All = 1.375,
                        Unit = Unit.Centimeters
                    }
                },
                Objects = {
                    new ObjectSettings
                    {
                        HtmlText = "<html><body><h4>my content</h4></body></html>",

                        HeaderSettings = new HeaderSettings{ HtmlUrl = headerPath },
                        FooterSettings = new FooterSettings{CenterText="footer"},
                        CountPages = true,
                        ProduceExternalLinks = true
                    }
                }
           };

            byte[] result = converter.Convert(document);
            System.IO.File.WriteAllBytes(@"E:\Learning\pdfoutput.pdf", result); 
renjithrainy commented 8 years ago

this is the output file pdfoutput.pdf

renjithrainy commented 8 years ago

The issue is not with printing ascii character, but the issue seems to be related to with header positioning. I see that the header and content comes when i use Pechkin but the content is not visible now. Please let us know if you have any thoughts. I am still working on it. Plz see following code:

InitializeTuesPechkinConverter();
            GlobalConfig gc = new GlobalConfig();
            gc
              .SetDocumentTitle("Test document")
              .SetPaperSize(PaperKind.Letter);
            IPechkin pechkin = Factory.Create(gc);

            ObjectConfig oc = new ObjectConfig();
            oc.SetCreateExternalLinks(false);
            oc.SetFallbackEncoding(Encoding.ASCII);
            oc.SetLoadImages(false);

            oc.Header.SetHtmlContent("E:/htmlcontent.htm");

            oc.Footer.SetLeftText("[page]");

            try
            {
                byte[] result = pechkin.Convert(oc, "<html><body><table><tr><td>My Website</td></tr></table></body></html>");
                System.IO.File.WriteAllBytes(@"E:\Learning\pdfoutput.pdf", result);
            }
            catch (Exception ex)
            {

            }
tvolge commented 8 years ago

We also have the same problem. We can not add the header via 'SetHtmlContent'. Although I noticed, that body content is sometimes shown if html is structured with divs...

Did you find the solution?

tuespetre commented 8 years ago

@tvolge you may need to watch the converter for an Error event. If you are attempting to pass in a file path to which the running process cannot obtain access, it may cause some problems.

tvolge commented 8 years ago

@tuespetre an Error event doesn't trigger, nor Warning event. If header and footer consist of simple and short HTML like <div>foo header</div> they show up, BUT main html content does not. If I don't speciffiy the header html, then the main html content shows. If header is more than only one simple div, it does not show.

Any idea / rule about specific html structure that won't cause this behavior?