How to produce documents (docx or pdf) from SQL Server? - c#

I know this is a little subjective, but I'm looking into the following situation:
I need to produce a number of documents automatically from data in a SQL Server database. There will be an MVC3 app sat on the database to allow data entry etc. and (probably) a "Go" button to produce the documents.
There needs to be some business logic about how these documents are created, named and stored (e.g. "Parent" documents get one name and go in one folder, "Child" documents get a computed name and go in a sub-folder.
The documents can either be PDF or Doc(x) (or even both), as long as the output can be in EN-US and AR-QA (RTL text)
I know there are a number of options from SSRS, Crystal Reports, VSTO, "manual" PDF in code, word mail merge, etc... and we already have an HTML to PDF tool if thats any use?
Does anyone have any real world advice on how to go about this and what the "best" (most pragmatic) approach would be? The less "extras" I need to install and configure on a server the better - the faster the development the better (as always!!)
Findings so far:
Word Mail Merge (or VSTO)
Simply doesn't offer the simplicity, control and flexibility I require - shame really. Would be nice to define a dotx and be able to pass in the data to it on an individual basis to generate the docx. Only way I could acheive this (and I may be wrong here) was to loop through controls/bookmarks by name and replace the values...messy.
OpenXML
Creating documents based on dotx templates, even using OpenXML is not as simple as (IMHO) it should be. You have to replace each Content control by name, so maintenance isn't the simplest task.
SSRS
On the face of it this is a good solution (although it needs SQL Enterprise), however it gets more complicated if you want to dynamically produce the folders and documents. Data driven subscription gets very close to what I want though.
Winnovative HTML to PDF Convertor*
This is the tool we already have (albeit a .Net 2.0 version). This allows me to generate the HTML pages and convert those to PDF. A good option for me since I can run this on an MVC3 website adn pass the parameters into the controllers to generate the PDF's. This gives me much finer-grained control over the folder and naming structures - the issue with this method is simply generating the pages in the correct way. A bonus is that it automatically gives me a "preview"...basiclly just the HTML page!

Office OpenXML is a nice and simple way of generating office files. XSLT's can be strong tool to format your content. This technology will not let you create pdf's.
Fast development without using any third party components will be difficult. But if you do consider using a report server, make sure to check out BIRT or Jasper.
To generate pdf's I have been using the deprecated Report.net. It has many ports to different languages and is still sufficient to make simple pdf's. Report.net on sourceforge

I dont think SQL Server itself can produce pdf files. What you can do is, as you mentioned, install an instance of SSRS and create a report that produces the information you need. Then you can create a subscription to deliver your report to where you want, when you want.
Here is an example of a simple subscription:

Go for SSRS only if you are OK with setting it up on a server and there is a definite need for schedule reporting and complex reports.
If you have code for manual PDF/docx generation, I would suggest to go ahead with it. Hopefully the complexity of its code is not a matter to you.
I have used both in separate scenarios. We used excel classes and objects from .NET for a minimal reporting from a web application.
But went for an elaborate reporting scheme for a system which required 1000s of reports to be generated in a scheduled manner and delivered to selected set of people.

Related

How to convert office file to image

I am searching from last two days but did not find any thing.
My requirement is to create a document viewer in my web application (C#.Net) and I don't want to use any third party tool for this. Can I convert the files in image or PDF or in any common formate which can be easly render on web page. I also can not use Introp object.
Any help will be highly appreciated
You mention in one of your comments that you'd like to write all the code yourself but don't know where to start. Here's how I would go about it...
First, you'll need to familiarize yourself with the Microsoft Office Format specification. You can find that here (there's a link to the technical specification). Office documents are actually a .zip file with an XML file inside along with any binary data representing attachments. Just renamed a .docx file as .zip and you'll be able to open it up and see the XML and any other supporting documents inside (same is true for xlsx, etc...).
Then you'll need to become intimately familiar with either PDF or HTML, as your job now will be to convert the various Office document structure into PDF or HTML structure, being sure to respect page layout, margins, order, etc...
As others have said, this is a large task which is why third party tools exist today. Also, each third party toolset has it's limitation as this is really hard to "get right" in all situations and there will be edge cases that work for one document and not another (because maybe they didn't use Microsoft Word to save the .docx, maybe they used OpenOffice and OpenOffice interpreted the standard slightly differently...)
If you cannot use COM/Interop technologies in your solution, you can take a look at the specialized 3rd party options. I see that you prefer not to use them, however, there are no existing built-in solutions in the .NET Framework. Check out my answer in a similar thread that describes how to accomplish exactly the same task using 3rd party libraries (for example, DevExpress, since I have experience with it). In addition, take a look at the Documents demo, where you can see how to create images/thumbnails from different types of MS Office documents.
I believe what you need is an intermediate representation of the documents which can be converted into an image for the viewer to display.
Lets me try to explain with the below diagram:
You can use tools like smallpdf or OfficeToPDF to do that. Just integrate them into your application.
Small PDF(https://smallpdf.com/library-detail)
officetopdf (https://officetopdf.codeplex.com/)

Best way to generate PDF in c# using Word or InDesign?

I'm comfortable generating Word documents using Aspose.Word (which can also save as a PDF) but I've recently been asked to do the same thing using a PDF as the starter template. We recently bought Aspose.Total and whilst Aspose.Pdf looks like it can do some manipulations it doesn't look to be all that flexible/easy (like adding a big line of text and getting it to wrap, and shifting other content down the page if it takes up more space).
What would be the best way of using a PDF as a template for what is basically a bit like a mail merge from a database? Should I turn it into a PDF form and merge it from an XML data source? Is this even viable or would such a form still have a limitation on spacing (so that longer lines/paragraphs of text won't reflow the document where necessary)?
From what I can tell it doesn't look like InDesign can be manipulated in c# even via a COM object (which would be nasty on a web server anyway).
If I recreated the InDesign/PDF as a Word document I'm sure I could work wonders, but you know what these publishing types are like, who think Word documents are the tool of the devil. These PDFs are never going to a professional printer anyway; they're just brochures for a client to download from a web page (based on information in a database) for printing/use at home.
You have indeed many solutions for such a web to print project. Choosing one is a matter of budget, requirements and users count. Placing dynamic contents can be done at the simpliest with PDF forms fillable with xml data.
On the other hand you can work with InDesign Server and output PDF based on InDesign templates. That's generally a good choice when a large amount of users needs to get rich pdf files in parallel. But the costs are heavy.
You can also envision A pitstop server or Callas PDFToolBox Server to place dynamic texts based on variables as supplied by you. The good point here is that you don't need much coding here. Those apps are ready to use.
You can at last consider command line tools. A few of them may have some useful commands such as pdfTk or cPdf to merge texts.

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.

Is there a pre-existing sulution using c# with the Excel Interops to template excel like jXLS does?

I'm looking for comments, alternative solutions and recommendations.
Background:
We currently have a CRUD app built on Websphere using the struts framework. We generate excel documents using a standalone C# application that is tied together to the web application using an ActiveX control. We pass XML that contains elements for each document. Under each document we have placeholders that contain a name and a value. The document generation application takes the name (either a cell reference or a named range) and a copy of the template from a file server. It then adds the data to all of the specified locations in the document and saves the document to the users "My Documents" folder. It then attempts to upload the documents to Documentum.
Current Problem:
One of the templates has grown to very large sizes. For the most part this is because of the amount of data that needs to be displayed. We breakdown the information into groupings. Each of these groupings needs to be able to display information for up to 20 pieces. Some of these tabs also need to display up to 100 sub elements for the pieces. Due to the current nature of the home built document generation framework, it's not possible to dynamically generate the different sections with a variable number of elements. This has recently started to cause problems with the maintenance of the template. If we need to make formatting changes, it needs to be replicated to not only every tab, but possibly for every instance within each tab.
I started to look at jXLS and it's templating abilities, but since it's java based it will be a hassle to integrate it with the c# application. The other alternatives would be to build jXLS like capabilities into our generation framework, integrate a third party library into the framework, or do the document generation on the Application server. I'd prefer to avoid reinventing the wheel, and would like to re use a library from somewhere.
I'd also like to not impact the existing work flow as much as possible. So the prefered order of solutions right now looks something like this.
1) use an Existing Library for .NET
2) Call the jXLS library from the DocumentGeneration framework.
3) Build in the templating capabilities to the Generation Framework
4) Use jXLS on the application server (Due to the way we have the process upload to Documentum)
What recommendations do you have for this?
I am not sure how much this will help you, but I have used NPOI in the past for generating XLS documents and it is quite easy to work with. NPOI is a .NET port of the Java POI, POI is used by jXLS internally.
I am not sure if it's functionality will suite your requirements, or at least be a starting point.
I've used Syncfusion's XLSIO component for excel templating along the lines of JXLS. It's .net and works pretty well, but it is a commercial product and licensing becomes expensive if you have lots of developers on the project.
If you find an open source port of JXLS do share!

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