Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Use case of a publishing house. Handling files operations module

Use case of a publishing house. Handling files operations module

How to get the pages number in a PDF file selected by the user, and preview it.


It is known the "Calculated Fields Form" plugin is constantly evolving to include new operations and expanding the base of projects where it can be used.

This post describes a hypothetical and basic publishing house use case for self-published books.

Project description

We will implement a form that allows users to upload a PDF file, select the type of cover (soft or hard), as well as the type of paper. Upload the cover image. Enter the number of copies to be printed.

The form should extract the number of pages from the PDF file, as well as calculate the size of the image, in order to use this information to calculate the cost of the service.

The form should calculate the cost per copy using the following rules:

  • 2 cents per page for white paper and 4 cents for glossy paper.

  • Increase the cost by 5 cents for soft cover and 15 cents for hard cover.

  • Add 1 cents per copy if the cover image is smaller than 250,000 square pixels and 2 cents if it is larger.

  • The total service's cost is calculated multiplying the cost per copy by the number of copies to be printed, being 100 copies the minimum allowed.

The form should preview the PDF file selected by the user.

Form's fields

Form Structure

The entry fields in the form would be:

  • A "Upload File" field to upload the PSDF file (fieldname1)

Upload File field

  • A "Dropdown" field to select the paper type (fieldname2)

Paper type

  • Another "Dropdown" field to select the cover type (fieldname3)

Cover type

  • A second "Upload File" field to upload the cover image (fieldname4)

Upload file for cover

  • A "number" field to enter the number of copies to print (fieldname5).

Copies to print

The output field.

  • Calculated field to calculate the service price (cost per copy x number of copies) (fieldname6)

Calculated field

The equation code is:

(function(){
    var total = 0;
    if(fieldname1)
    {
        total += PDFPAGESNUMBER(fieldname1|n)*fieldname2;
        total += fieldname3;
        if(fieldname4)
        {
            var size = IMGDIMENSION(fieldname4|n);
            total += IF(size['width']*size['height']<250000, 0.01, 0.02);
        }
    }

    return PREC(total*MAX(100,fieldname5), 2);
})()

The equation uses the PDFPAGESNUMBER and IMGDIMENSION operations in the "Handling Files" operations module to get number of pages in the PDF file and size of the cover image, respectively.

  • Finally, the form includes an "HTML Content" field to preview the PDF file.

Viewer