Form designer: How to generate a PDF version of the form archive


#1

This article provides a solution to generate the form archive as a PDF. The HTML version of the form archive is converted to PDF using an external library (WkHtmlToPdf).

Note: This solution has not been tested with a high number of transactions.

  1. Install WkHtmlToPdf (http://wkhtmltopdf.org/). Advantages:

    • 32-bit and 64-bit versions are available.

    • Good CSS support

    • License L-GPL (free)

  2. Create a FORM_PRINT process data (FILE data type). This process data will contain the PDF version of the form archive.

  3. Add a form section with two text fields with the IDs FORM_DRAFT and FORM_PRINT. You must hide this section for all workflow actions (edit the section Settings / Section Behavior / Hidden checkbox).

  4. Map the FORM_PRINT textbox to the FORM_PRINT process data. For each action of your workflow you have to map FORM_PRINT textbox to the FORM_PRINT process data. You can use the action parameters.

  5. In the .NET editor, add the following code:

    protected void Page_Load(object sender, EventArgs e)
      {
            base.Page_Load(sender, e);
            this.FORM_PRINT.Text = "form_print.pdf";
      }
    
      protected override void Render(HtmlTextWriter writer)
      {
            base.Render(writer);
    
            if (this.FormArchive != null)
            {
                HTML2PDF(this.FormArchive, this.StoragePath + "\\form_print.pdf");
            }
      }
    
      private void HTML2PDF (string HTML, string Path)
      {
            System.Diagnostics.Process p;
            System.IO.StreamWriter stdin;
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
    
            // run the conversion utility
            psi.UseShellExecute = false;
            psi.FileName = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe";
            psi.CreateNoWindow = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
    
            string args = "-q -n - " + Path;
            psi.Arguments = args;
            p = System.Diagnostics.Process.Start(psi);
           
            try
            {
                stdin = p.StandardInput;
                stdin.AutoFlush = true;
                stdin.Write(HTML);
                stdin.Close();
    
                if (p.WaitForExit(20000))
                {
                    Response.BinaryWrite(System.IO.File.ReadAllBytes(Path));
                }
            }
            finally
            {
                p.Close();
                p.Dispose();
            }
    
        }
    

Alternative libraries

It’s also possible to use a commercial component such as EVO or HiQPdf.

Example with EVO

EvoPdf.PdfConverter pdfConverter = new EvoPdf.PdfConverter();
pdfConverter.PdfDocumentOptions.PdfPageSize = EvoPdf.PdfPageSize.A4;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = EvoPdf.PdfCompressionLevel.Normal;
pdfConverter.JavaScriptEnabled = false;
pdfConverter.SavePdfFromHtmlStringToFile(this.FormArchive, this.StoragePath + "\\form_print.pdf");

Example with HiQPdf

HiQPdf.HtmlToPdf htmlToPdfConverter = new HiQPdf.HtmlToPdf();
htmlToPdfConverter.Document.PageSize = HiQPdf.PdfPageSize.A4;
htmlToPdfConverter.Document.PageOrientation = HiQPdf.PdfPageOrientation.Portrait;
htmlToPdfConverter.Document.Margins = new HiQPdf.PdfMargins(5);
htmlToPdfConverter.ConvertHtmlToFile(this.FormArchive, this.StoragePath + "\\form_print.pdf")

How to generate an Excel file from a form’s gridview
#2

In trying this, the pdf being generated is blank. Any tips or suggestions? We are using PDFSharp (1.32.3057).
Is FORM_PRINT to be mapped as both the IN and OUT parameters? How is the hidden FORM_DRAFT field used?


#3

Hi,

This article is regarding WkHtmlToPdf. Each solution should have a different approach to implementing them with WorkflowGen.

If you want assistance regarding the implementation of PDFSharp, kindly open an assistance ticket in our Helpdesk website and we would be happy to help.

Regards,
Eddy.


#4

Interesting… I did something similar to that. What would be ideal is if we can use the default FORM_ARCHIVE to store the pdf file. That way, by default, this pdf file will be attached to notification emails!

Anyway of modifying your solution to allow that?

Thanks


#5

This is already possible by using WkHtmlToPdf. All you need to do is enable “Show the data in the follow-up forms” to the data that stores the PDF file (In Data page -> Edit Data -> Visibility), and a link to that file should be generated in the follow-up form as well as the email that contains a list of all the data with “Show the data in the follow-up forms” enabled.

Regards,
Eddy.


#6

I would still think it would be smarter to have the default FORM_ARCHIVE because of the previews. When you preview a form on the dashboard, it shows you the form archive.


#8

Does this have to be installed on the computer of every user that needs to print the forms?


#9

Hi @athomas,

No, this only needs to be installed on the server where the WorkfowGen application is installed.

Regards,
Eddy.