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

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")

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?

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.

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

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.

1 Like

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.

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

Hi @athomas,

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

Regards,
Eddy.

Bumping an old topic here, but I’m trying something tangentially related to this and I’m wondering if this logic still works. I’m trying to save the FormArchive at a specific step in the workflow using .cs, and it seems like this.FormArchive is blank whenever I try to get at it. I did find this.GetFormArchive(), but it seems like that is trying to insert the archive into the existing form instead of just save it.

Is there an easy way to just export the FormArchive? Thanks!

Hi @isen0011,

this.FormArchive should be called at the overrided Render method:

 protected override void Render(HtmlTextWriter writer)
  {
     base.Render(writer)
     //Code here
  }

It is at this stage of the page’s life cycle where the FormArchive file gets generated.
So if you’re trying to get it at the PageLoad method, it will be empty.

Regards,
Eddy.

We have implemented the code listed above and successfully are able to output a PDF document using wkhtmltopdf. This seems to be working well for unless there is text within the form that is using expanding text boxes. The form output in PDF form shows the first line and not the additional lines of text. How can we use our expanding text boxes with the wkhtmltopdf?