How to merge data fields from a DOCX file with a data source and output to PDF

This article explains how to get the fields from a template DOCX file, fill them with data and merge to new PDF file using GroupDocs.

Requirements

We already know how to install the Fatfree framework and prepare for creating the sample from the previous article. The sample that we create in this article is not much different from other sample with Fatfree. We need a template file with a form and a controller file to process entered data.

Sample creation

The template file is the same as in the previous article. Because of this, I will not explain the template file but just show the code.

Template file code

OK, what's happening with the template file clear so lets take a look at the controller file.

Controller file

We already know how to create this file. If you're unsure, look at a previous post that explains how.

Below, I'll show you the complete controller code and then explain how it works.

That's the controller: open your mind and plunge in.

First of all, we get all the data transfered from the form by POST to the controller. This block of code does that:

As you can see, we use the Fatfree method F3::set to initially set the template variables and then the F3::get method to get the posted data. We have all necessary data to create API objects; the rest of the data we will get later.

Let's create a function that takes this data, checks it and does the rest of the magic. The function is called mergeFields and a definition looks like this: function mergeFields($clientId, $privateKey). The mergeFields function takes two parameters - client ID and private key. This data is most important because without it, we can't send a request to the API. That's why we check that it's been entered with this code:

Then, if the client ID and private key have been entered we proceed with API objects creation. I expect you already know how to do this.

Next, we check whether the user wants to upload a local file, one from the web, or a file already in the GroupDocs account. We can work with documents through the file GUID. We dont have this data, but that's not a problem - lets get it. Simply use the Fatfree method F3::get to get the client ID and private key.

Get the rest of the data:

Since this data are simple strings - with the exception of $file - check which of them are chosen by elementary verification on an empty string: if ($url != "") or if ($fileId != "").

If you wonder what to do if a user selects to upload a local file, how to check it. It's not a problem. In the global variable which consist of the local file we have an array of file data such as file name, temp name and file content. To check if a user has selected a local file, simply check if the file name is empty. This array field consist of string data and we can check it for empty string: if ($_FILES['file']["name"] != "").

According to which check returns "true", we know whether to use an entered file GUID or upload a file in three different ways. How to do this you can read in last weeks post: Two ways to upload files.

The most interesting part, and culmination of this example, is to get the field from template DOCX file and merge them to a PDF file. To do this, we need this GroupDocs PHP SDK methods:

  •  From DocApi GetTemplateFields
  • From MergeApi AddDataSource and MergeDatasource
  • From AsyncApi GetJobDocuments
  • And from StorageApi GetFile

First, let's take a look at the code which will do all this work and use our methods:

The logic is simple.

  • First, we get all the fields from the template DOCX file with the GetTemplateFields method. It takes the parameters client ID and template DOCX file GUID. The method returns an object with fields.
  • Now, create a DataSource object in which one of the parameters is a DataSourceFields array which we must create. We will put the field we get from template DOCX file into this array. This action occurs in this peace of code:

As you can see, the DataSourceFields object has a values parameter. Into this parameter we put an array of strings or only string which will be added to the field as content.

  • After creating the DataSource object, the next step is to add DataSource to GroupDocs for the convertion to PDF. This trick can be done as follows:

In response to this we get the datasource ID by which we can both merge it and convert to PDF, thus killing two birds with one stone.

  •  Take the client ID, datasource ID, the original DOCX file GUID and put all of this into the MergeDatasource method. This request in code:

As you see, one of the input parameters is a string with type of the result file. When the API gets this request, it creates a new PDF file with merged fields.

Phew, we merged our fields to a new PDF file and now we can download it and see it in an iframe. The MergeDatasource method returns Job id using which in the GetJobDocuments method we get all the info about the new PDF file that we need, such is GUID and name.

  • To download it, create a FileStream which contains a local path for downloading the file:

After that use the GetFile method to download it: $download = $apiStorage->GetFile($clientId, $guid, $outFileStream);

You already know how to generate an iframe.

The only thing left is to call our function and return the new PDF file GUID to the template to show in the browser as an iframe. You already know how to do this too, if you've followed this article series.

And we're done!

Screenshots

Here are two screenshots that show the input form and the output file so you can see the process in action.

The input form

form How to merge data fields from a DOCX file with a data source and output to PDF

The output PDF

iframe How to merge data fields from a DOCX file with a data source and output to PDF

Two ways of uploading files into a GroupDocs account

Working with GroupDocs SDK we usually need a GUID to work with a document. But it's not very comfortable for users to work with GUIDs. So in your app, you can use two methods to upload a file to a GroupDocs account and get it's GUID programmatically.

Requirements

We already know how to install the Fatfree framework and prepare for creating the sample from the previous article. So lets start with the magic.

Theory

Upload local file

To upload a local file we need an actual file. You can view many types to word processing documents (DOC, DOCX, TXT, RTF, ODT), presentations (PPT, PPTX), spreadsheets (XLS, XLSX), portable files (PDF), and image files (JPG, BMP, GIF, TIFF).

From GroupDocs PHP SDK we will need the following methods:

  • Upload
  • FileStream::fromFile

The template is simply an HTML form of the "multipart/form-data" type. This form sends date entered by the user (the client ID, private key and local file data) to the controller. The controller, in turn, gets the data, prepares it for an API request, makes the API request and receives the upload results from the API with info about the uploaded file.

Upload file from web

If you think this is going to be complicated, I have to disappoint you. With GroupDocs PHP SDK it's really simple than upload a local file.

To upload a file from the web, we need a direct link to the file. Everything else is the same as when uploading a local file, with the exception of the upload method. To do this trick we need only one method: uploadWeb. The logic of web uploading is the same as for local file upload.

Since the logic is the same we'll use same template and controller.

Practice

As before, create a template file in the template folder and a controller file in the inc_samples folder.

Template file code

Let's investigate this code. This is a simple HTML document with JavaScript. To select the file source (local or web) we've added a switch:

This switch calls the JavaScript function display. This function receives one parameter which specifies the source of the file: local or web. Where local = upload local file and url = upload file from web. Toggling the switch displays the corresponding input field.

OK, it's clear how the template works - it's simple enough. Let's take a look at the controller.

Controller code

Now let's analyze the blocks of code we have here.

In this block we get the data from the form transfered by the POST method with the Fatfree method F3::get which gives access to the global arrays such as POST, GET, FILE and SERVER. When we have collected the entered data, we can create a function which will do the job and we will have the ability to check every steps and get errors if there are any. Our function will get four parameters: client ID, private key, width and height (we need width and height for the iframe that will show the uploaded file).

First of all, we must check whether the client ID and private key are entered. Without this data we can't do anything. Check it with this code:

If we have this data, we can proceed to the next code block:

Here we set user data for template and get the rest of the entered data from the form, such as the URL of a web file and the location of the local file. Later, we will check which fields are chosen by this data. Now we must create objects which will set up the relationships with the GroupDocs API and user account. This code does that:

So we can check from where we're about to upload the file and then upload it. To check if the user choose to upload a file from the web, and to upload the file, use this code:

This block does all the magic uploading a file from the web. As you can see we check whether $url is an empty string. If it is not empty, the user has selected to upload a file from the web and we use the URL. To upload the file, we make a request to the UploadWeb method with the client ID and the URL for the file:

Thats all for web uploading. Simple, yes?

OK in response to this method we have an object with an array of all the info for this file such as the GUID, name, type and so on. To show this file in an iframe, we only need the GUID. We can get GUID easily:

If uploadWeb fails we get an error message:

But what if the user wants to upload a local file? We can check if they do, and if so upload the local file with this code:

To upload a local file we must get its name, assign it a temporary name and convert it to a FileStream. The first two we can get as usual for multipart/form-data:

And to convert the file to FileStream in the GroupDocs PHP SDK we have very cool thing: FileStream::fromFile. This function gets the file's temp name and returns a FileStream. Now we can send request to the GroupDocs API to upload the local file. The Upload file method get such parameters as client ID, local file name, description(string), callback URL (in this sample we'll use an empty string but if you want to, you can specify it) and FileStream. In the response object for the UploadWeb method we get info about the uploaded file. If the request failed, we get an error message.

Displaying the file

We've finished the upload and now we have only to generate an iframe and call our function.

 

The code in action

Thats all. Below are a couple of screenshots that show how it looks in action.

The input form

form1 Two ways of uploading files into a GroupDocs account

 

The iframe showing the uploaded document

iframe2 Two ways of uploading files into a GroupDocs account

 

GroupDocs November, 2012 Newsletter – GroupDocs Viewer and Annotation supported on even more platforms

GroupDocs November, 2012 Newsletter - GroupDocs Viewer and GroupDocs Annotation supported on even more platforms

In October, we launched a number of new plugins for GroupDocs Viewer and GroupDocs Annotation. There are more to come as we'll continue to design and launch plugins in the next few months. We haven't forgotten about our other users: we're also working on the apps at GroupDocs.com and are making great changes to some of our apps.

title july GroupDocs November, 2012 Newsletter   GroupDocs Viewer and Annotation supported on even more platforms

This month, we've launched a large number of plugins as well as updates to the GroupDocs SDKs.

coming soon GroupDocs November, 2012 Newsletter   GroupDocs Viewer and Annotation supported on even more platforms

Expect these exciting features and updates in the near future:

  • Launch of GroupDocs Assembly v2.0(currently in beta).Adds a new workflow and a lot of improvements to make GroupDocs Assembly easier to use.
  • Launch of GroupDocs Annotation v2.0(currently in beta).Adds new tools and features that makes collaboration even easier.
  • Integration:
    • Integration with SalesForce.
    • Integration with Zapier.com.

Community buzz GroupDocs November, 2012 Newsletter   GroupDocs Viewer and Annotation supported on even more platforms

Featured blogpost GroupDocs November, 2012 Newsletter   GroupDocs Viewer and Annotation supported on even more platforms

Good news for Orchard CMS users! We’ve introduced a GroupDocs Annotation app plugin for Orchard. This plugin lets you embed GroupDocs' efficient online document annotation app as well as selected documents to Orchard pages. Once the document is embedded, you can annotate the document using GroupDocs' efficient annotation app. Annotate your documents using easy-to-use tools or perform document collaboration online by sharing documents with your colleagues. Read more.

Thank you for choosing GroupDocs.

The GroupDocs Team

What is GroupDocs Assembly?

Last week, I mentioned changes that we’re making to GroupDocs Assembly to make it more intuitive. Document assembly (also known as document automation) is an aspect of document management that you might not be familiar with, so this week I’ll discuss what it is and how it can be used in more detail.

Document management?

You’ve found your way to GroupDocs and the GroupDocs blog so chances are that you have an idea about document management. It’s simply the process of keeping track or and managing the various documents you use. Creating a professional-looking template, storing it online and sharing it with your colleagues so that they can all use it is all document management. So is customising contracts, sending them out for signing and archiving the signed documents.

Read More

Introducing GroupDocs Assembly’s new look and feel

We're continuing work on the new and improved GroupDocs look and feel. We're told you that we're making changes to the GroupDocs dashboard and GroupDocs Viewer, and we're also changing GroupDocs Annotation. We're not stopping there. We're also making changes to GroupDocs Assembly.

GroupDocs Assembly is an app that lets you automate document assembly and automation.

Think of your document as a form that you can use to create several new documents from. You start with a template document. In the template, there are a number of fields that will collect information, for example, name, address, date. (These are straightforward examples but you can use GroupDocs Assembly to create really sophisticated forms and collect all kinds of data.)

Read More

Keep up with what we’re doing: read the GroupDocs newsletter

GroupDocs changes regularly as we introduce new features and update the interface. It's easy to keep up with what's happening:

  • Follow this blog. We post weekly, sometimes even more often, with recent and upcoming changes.
  • Read the GroupDocs newsletter. On the first of every month, we send out news about what we've been up to in the last month. We share what new features we are planning, our most popular blog post and any media we've had.

The May 2012 GroupDocs newsletter is our first issue. In it, you'll find out what we're working on right now, what new features we've released recently, what other people are saying about us, and what we're saying in this blog.

Read More

Document collaboration – introducing the new look GroupDocs Annotation

As you might have seen from our recent posts, we're working hard to change the look and feel of GroupDocs. We're working on the GroupDocs dashboard and we're making changes to GroupDocs Signature. We hope you'll agree that it is all for the better.

Today, I'm writing about GroupDocs Annotation, a service that makes document collaboration easy. With GroupDocs Annotation you can work on a document with your colleagues, all at the same time. You don't have to be online at the same time, making suggestions and adding comments, but you can be. If you're more comfortable with traditional document collaboration, when everyone takes their turn, that works just as well.

GroupDocs Annotation collects all the document collaboration tools that you need together in one handy place. Here's an overview.
Read More

Managing digital signatures: introducing the new GroupDocs Signature dashboard look and feel

Earlier this week, we announced that the GroupDocs dashboard is getting a makeover to make it even easier to use. The dashboard is not the only of our tools that we're reviewing. We are also working on improving the way the GroupDocs Signature dashboard looks and works. The GroupDocs Signature dashboard is used to collect and manage digital signatures (also known as electronic signatures). Here's a preview of what's coming in the next few weeks.

A new interface for working with digital signatures

The new GroupDocs Signature dashboard looks a lot like the GroupDocs dashboard so it will feel familiar to GroupDocs users. Drag and drop documents to create a new envelope. See how documents are progressing at a glance with the colour-coded progress indicators. Sort envelopes by documents, recipients, date or status. Navigate with the breadcrumb trail.

envelopes a Managing digital signatures: introducing the new GroupDocs Signature dashboard look and feel

The new GroupDocs Signature dashboard.

Read More

Introducing new GroupDocs Signature features

Recently, I wrote about some changes we're making to the GroupDocs Signature dashboard. That's not the only part of GroupDocs that we're working to improve: today, I'll tell you about what we're planning for GroupDocs Signature.

GroupDocs Signature is an easy to use electronic signature service. You use it so collect signatures without the administrative overhead of printing, stuffing envelopes and posting. Simply email documents to your signers and wait for them to sign. GroupDocs Signature even reminds signers if they forget to sign so that you can focus on other things.

Read More

Introducing the new GroupDocs dashboard and document viewer

We're about to launch a new GroupDocs dashboard and document viewer. We have overhauled the look and feel of the tools to make them more intuitive to use. At the moment, we're adding the final touches and making sure that everything works just so. The changes will be live in the next few weeks.

Here's what will change when the new dashboard goes live:

  • GroupDocs Viewer
  • I'll describe each of the new features in more detail below.

    Read More

    Previous
    1. 1
    2. 2

    Back to top