Create PDF from HTML form results in C# - c#

I have a project where I need to create an HTML form (no problem) and then create a PDF file from the results using C#.
I have done this before in PHP using FPDF but this one needs to be C#. Ideally I want to put the code into a user control and then stick it in an Umbraco website.
Can anyone recommend a good way to do this? PDF doesn't need to be fancy, it'll just display text, we aim to create a generic purchase order based on what the customer wants from the form, which can then be emailed to them to print off on headed paper.
Thanks

There are a couple of recent problems with iTextSharp. The most annoying is that in the latest version they've deprecated the HTML parser. So now everything has to work through the XMLWorkerHelper singleton and parses through ParseXHtml. I find this a real pain, since HTML pages which aren't well formed appear fine on browser, parse OK in the old method and now crash out with an exception. So it necessitates an extra step to make sure your HTML is well formed (as XHTML) first. If you are generating your HTML from an ASPX page, then using Server.Execute() to get the stream, then this might be useful to you for iTextSharp:
http://jwcooney.com/2012/12/30/generate-a-pdf-from-an-asp-net-web-page-using-the-itextsharp-xmlworker-namespace/
Be mindful that iTextSharp has a distinct lack of any decent documentation of the modern changes (being mindful that the Java iText documents don't translate perfectly to C#), it makes the learning curve far too long and steep for any practical use in short spaces of time. I've basically given up on that platform, though may just create a baseline system to get something working lean whilst I then learn another framework.
As a result, I'm looking at PDFizer and PDFSharp libraries. If I have some success, I'll report back.

here is a library for converting HTML to PDF
http://pdfcrowd.com/web-html-to-pdf-net/

I like the PDFsharp library. Not sure how it would work for your needs, though.

Related

Rationale behind reporting framework

In the enterprise world it seems that if you want to display a report, you have to use a reporting framework. You name it: Crystal reports, Jasper reports, ActiveReports, SSRS. But I fail to see why, in 2015, you should prefer a reporting framework over generating an HTML page server side. In a .NET application, for example, what is the advantage of using such framework over a simpler approach of using razor to generate an html web page? That page can be then printed to generate a pdf.
It seems to me that there is a perceived difference when the word "report" is mentioned, that for some reasone requires a separate framework.
Of course usually a reporting framework offer additional features, like a report viewer, but in many cases it's used just to generate a pdf. The advantage of using html+css+js over any other proprietary language to describe a report are to me uncountable: more maintainable, easier to read and write, html is a standard understood by anyone, and there is no need to add another piece to the technology stack of your web service.
I understand my question up to now can be seen as not conforming to SO guidelines, so I'll try to make a more precise question rather than a rant against the enterprise world.
Question
Let's say my use case is to generate a report like this one:
inside a page or an iframe. What are the advantages of using, say, Crystal Reports or ActiveReports, in an ASP.NET web site instead of using Razor rendering as I do for all the rest of my service? Is there anything I should be more careful when generating reports compared to normal reports? If the bonus I get is that crappy top bar at the top of the page, well, I would gladly get rid of the pain of using a Reporting Framework.
So you generate an HTML page. How are you going to print it? Are you going to pull in a library to do it? What's your strategy for properly styling it and making sure that navigation elements don't end up in the printed report? How are you going to support exporting to Word, Excel, CSV, or PDF?
Reporting tools often take care of those details for you.
That said, in my own applications I usually generate PDF's with Razor PDF (and I tell people to print the PDF), generate Excel exports with EPPlus, Word documents with DocX, and don't offer CSV as I hate that format. It means I typically have to redo every single report for each format, but I like the control I get over the output.

Creating PDFs Online

We are using Report Definition laguage (RDL) templates to define various reports in one of our Sharepoint applications. These reports are (then) saved as PDFs into various SharePoint Document Library's. One report in-particular renders, but is considered to be "failing" due to the styling needs of the report. So it appears RDL only understand "very simple" HTML.
For Example:
Trademark characters are not rendering as superscript (they render as normal text instead)
The ability to assign Line Height fails
The ability to assign Word Spacing fails (so printers "leading" requirements fail)
Both of these point to various marked Microsoft limitation for RDL's to interprint various HTML...of which we are now aware.
So...
I need a better tool...and we are scratching our heads on this one!
QUESTION:
What tools take-in HTML, understand CSS (well!) and can generate PDFs from C-Sharp objects?
Please keep in-mind I need the to PDF generator tools you recommend (below) to understand CSS and HTML.
NOTE:
I looked at the various other StackEchange sites to see if there is a better forum for this particular question, but this one was the only one that seemed to fit-the-bill. If you are a mediator, and feel this question is mis-placed, please feel free to move this question.
This HTML to PDF converter has the most accurate conversion of a complex html/css page. There is also a demo to try the conversion with your html
Maybe you can give Amyuni WebkitPDF a try. It is a Free component for converting HTML+CSS into PDF files. From the home page:
Directly convert HTML files into PDF without the use of a web browser or a printer driver
Convert HTML files into XAML/XPS for rendering within Silverlight
Integrate and deploy the HTML conversion feature within your applications
Generate either a single continuous PDF page or split the HTML into multiple PDF pages
Amyuni WebkitPDF is distributed as a library with a sample application, and sample code for C++ and C#.
Disclaimer: I currently work as software developer at Amyuni Technologies.
I only know a workaround for the "leading space" issue. This example "leads" the value with 10 spaces:
=space(10) & Fields!FieldName.Value
This should work for any renderer, I'll update this if I come around other tricks.
Have a look at Aspose.Pdf for .NET: http://www.aspose.com/categories/.net-components/aspose.pdf-for-.net/default.aspx

Approaches to print a document in C#

From time to time we all need to print a document from our .NET programs. Let's say a simple one page document with some text and image.
So far I know 2 ways to do it:
Using standard PrintDocument class
Generate a pdf/doc document using such tool like PdfSharp
First way I don't like because it takes a lot of time to write all these routines for handling events and standard PrintPreviewDialog is just ugly. Beside that you need to generate and scale 2 different documents on print preview and actual printing.
Second way bothers me because these free components quite often are limited and poorly documented.
What other solutions for printing do you know?
use Crystal Reports for printing. Best Option IMO
I haven't seen an elegant printing solution in .NET yet, except for WPF and XPS. In that case, you can just create a UI in Xaml and print it. But XPS isn't often a viable option. I've worked with XSL-FO also (using NFop and later FO.Net - the latter is better because newer and it doesn't require Visual J#), which works but has a moderately steep learning curve.
I have tried to analyze possible options in the article pdf reports in c#.
iTextSharp - imperative way of doing reports in c#, (-) produce not very readable code
Telerik Reporting, DevExpress reporting, Crystal Reports - similar approach, report editor, great for simple job, able to produce multiple formats, (-) harder to team merge and print complex reports
phantomjs, wkhtmltopdf - html to pdf conversion, (-) extra dependency
jsreport - my solution, javascript templating engines to assemble html report and phantomjs to print it into pdf (-) overhead for simple solutions, extra dependency
Long story short. I was not satisfied with the current options and developed jsreport as a solution for designing and printing pdf reports. Check it out, it's free.

Programmatically find and replace text in pdf

I'm trying to create a web script that will allow me to alter PDF templates that I have uploaded and re-output them. I have tried Zend already which allows me to write to a PDF but that means leaving the PDF blank in certain space which is to primitive for what I need. PDFFlip was not any better.
We need to implement functionality so we can remove content from the PDF as well as remove and replace. I have looked at CAM::PDF and changepagestring.pl but I'm not sure it's up to the job. I was hard pressed to find any real usage examples and Perl is not a language I have used before.
This is for a web project but I am flexible with the language we use, ideally PHP or ASP.NET C# would be great. Preferably not Java unless there is no other way.
I should also point out that I looked through the FoxitReader SDK without any luck. I never tried to implement it but I found no mention of find and replace like functionality.
You can tinker with PDF text but it is not straight-forward just to search and replace. The text is designed as an end-file format not for easy editing. I wrote a blog post explaining some of the issues at http://pdf.jpedal.org/java-pdf-blog/bid/12670/PDF-text
May be as workaround it's better to hold and fill in templates in some format that is more convenient for editing? E.g., you can keep your templates as Microsoft Word templates and then export them to PDF after filling. This thread may be useful on this way.
PDF file format isn't quite appropriate for editing.
Alternatively, you may prepare your templates as PDFs containing form fields. In this case filling of form fields is common and well-known task and there a lot of pdf components for this.

Best way to print Invoices, Pick Tickets etc

Well, heres my scenario.
Client/Server winforms application with SQL Express as the DB. I need to be able to print invoice, packing slips etc..
i would like the customer to be able to modify the invoices. ie. be able to put their logo or change font sizes etc...basically format the display.
Things i have considered so far are.
1) Use a template engine (similar to codesmith or mygeneration) and use templates that output HTML. Then print the html page.
2) Use ReportViewer in local mode. I've heard that users can download a plugin for web dev express and edit the local report files. can anyone confirm this?
3) Use Reportviewer in remote mode.
I don't have much experience with ReportViewer so I'm not sure if i should use local or remote mode as well.
Those of you that have done this kind of thing before whats your recommendation?
After just completing a project with it, I would heartily recommend iTextSharp to create your invoices and other forms as PDFs. In addition to creating PDFs from scratch, you can also use it to fill in PDF forms and/or templates created with Acrobat (or even MS Office/OpenOffice). And it's free.
It's pretty easy to use in Windows apps or in ASP.Net applications. Most of the documentation and the books on it (iText in Action, for example) are about the original Java version, iText. However, there are tutorials and example code on the conversion process and, for the most part, all of the functions and libraries work the same in the .Net version, so adapting the book and reference code has been no problem.
I definitely learned the hard way that HTML and CSS are great for browsers (well, great except for the "every browser interprets it different" problem), but horrible for trying to generate consistent, attractive, and precise printed output and forms.
I'm personally using Aspose Words: they use word documents as templates, and I'm using Words bookmarks function to mark and retrieve the fields I need to fill.
Aspose works nicely with Tables (ie: you can add lines to a table, etc...) and sees Word documents as XML documents. You can then save the document as MSWord or PDF.
I wouldn't say it's the greatest library in the world, but it's definitely worth having a look :)
you can use Crystal Report for this. But first you need to scan the INVOICE and save it as an image,
Next is, on your crystal report, export the image on to it, and DRAG the fields to where they must print on the invoice (IMAGE SERVES AS YOUR GUIDE). Then after everything has been set-up, DELETE THE IMAGE and try it.
hope this helps.

Categories