Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8041349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:11:08+00:00 2026-06-05T04:11:08+00:00

I have a project in which we are using phpdocx pro to generate a

  • 0

I have a project in which we are using phpdocx pro to generate a .docx file in the from templates. I can get the data in to the template easy enough, however when the file is downloaded and opened in MS Word 2010, the program reports that the file cannot be opened because there are problems with the contents, the details being “The file is corrupt and cannot be opened”. Word can repair the document, however the issue still stands that it should not be corrupted in the first place.

This is how I’m generating the document:

function generateUnitDesign(){
  if($this->populateRecords()){
      require_once dirname(__FILE__).'/phpdocx/classes/CreateDocx.inc';
      $filename = 'UnitDesignTemplate-'.str_replace(' ', '', $this->rec->title);
      //Create Document
      $document = new CreateDocx();
      $document->addTemplate(dirname(__FILE__).'/templates/unitdesigntemplate.docx');

      // Fill in text fields
      $document->addTemplateVariable('TITLE', $this->rec->title);
      $document->addTemplateVariable('CHALLENGE', $this->rec->challenge, 'html');
      $document->addTemplateVariable('HOOK', $this->rec->hook, 'html');
      $document->addTemplateVariable('RESEARCH', $this->rec->research, 'html');
      $document->addTemplateVariable('AUDIENCE', $this->rec->audience, 'html');
      $document->addTemplateVariable('SUMMARY', $this->rec->project_brief, 'html');
      $document->addTemplateVariable('RESOURCES', $this->rec->resources, 'html');
      $document->addTemplateVariable('REQUIREMENTS', $this->rec->requirements, 'html');
      $document->addTemplateVariable('SCAFFOLDING', $this->rec->scaffolding, 'html');

      $document->createDocx($filename);
      unset($document);
      header("Content-Type: application/vnd.ms-word");
      header("Content-Length: ".filesize($filename.'.docx'));
      header('Content-Disposition: attachment; filename='.$filename.'.docx');
      header('Content-Transfer-Encoding: binary');
      ob_clean();
      flush();
      readfile($filename.'.docx');
      unlink($filename.'.docx');
  }
}

Originally, I was trying to use their createDocxAndDownload() function to get the file, but it would leave a copy of the .docx file on the server, which was not ideal. Am I missing something? Is there someone with more experience with phpdocx to lend a hand?

Edit:
Well, I feel like an idiot. After narrowing the issue down to the portion of code that outputs the file, I finally opened the file in a HEX editor and discovered the issue was that after the file was output successfully the web frontend would append the start of it’s HTML to the end of the docx file making a ‘corrupted’ file. This one line immediately after the unlink() fixed the whole thing:

exit;

Pekka: If you would like to answer this with the new information, I’ll accept your answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:11:10+00:00Added an answer on June 5, 2026 at 4:11 am

    After narrowing the issue down to the portion of code that outputs the file, I finally opened the file in a HEX editor and discovered the issue was that after the file was output successfully the web front end would append the start of it’s HTML to the end of the docx file making a ‘corrupted’ file. This one line immediately after the unlink() fixed the whole thing:

    exit;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two project, one which generates some artifacts from a WSDL file using
I have a legacy project using Rails 2.3.5 but I can't find with which
I have a project which needs to generate PDF documents. I am using iTextSharp.
I have a project in vc++ which using standerd windows library and source file
I have a project which I can deploy to 4 sites from within VS2010,
I have a Silverlight Project which passes data using the Data Contract to a
I have a project which I can build and deploy correctly using Ant I
I have just restarted a project in which I am using NHibernate. The project
I have a project which builds correctly from the command line. On one of
I have a project which is using DocumentFormat.OpenXml.dll 2.0.3920.0. However, I want this to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.