Categories > OpenTBS with DOCX >

download problem

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: dako
Date: 2016-05-24
Time: 18:35

download problem

Hi, this OpenTBS is amazin thing !
It solves many many printing problems in Web applications. Create Word or Excel template file, merge it with data and it is done.
How I did not now anything about it earlier ? Easy, I am fresh in Web, PHP, etc. :) .

I work locally using xampp v.3.2.2.
Prepared all necessary scripts to make .docx merged file. All works well. Created merged file without any errors.
Used $TBS->Show(OPENTBS_FILE, $output_file_name);
I got .docx file on server (localhost). Say it is sample.docx, in directory wordfiles under C:\xampp\htdocs\test10 folder.

After that, prepared PHP script to download that file from folder wordfiles (actually server) to client computer (on local disk, say in D:\wfiles folder).
Download finished, no errors. I got file sample.docx on local disk, in D:\wfiles folder.
Now, game begins. When I double click on downloaded file (Word 2010), a I got message that file is corrupted, need to be recovered.
When I accept to recover file, it opens with correct content and shape, as I expected to be.

Next is interesting: As I work with Xampp, I open C:\xampp\htdocs\test10\wordfiles folder, see (say original) sample.docx file and double click on it.
It opens without any error !!
Last thing: I did simple copy file from Xampp folder to D:\wfiles folder. Double click on, again it opens without any error ! What download could do to that file ?

Tried several download scripts I found on Internet, but same result.

When using $TBS->Show(OPENTBS_DOWNLOAD, $output_file_name), opening display same message as above, requireing to recover file.
After that, Word opens file with correct content (merged data appeared).

Did anyone experienced this situation ? Why it correctly opens in Xampp folder, but not after download ?

Thanks in advance and best regards to all.

Dako Simo.


By: dako
Date: 2016-05-25
Time: 14:16

Re: download problem, example documents

I am sending two files described in this post first message.

https://www.dropbox.com/sh/ur6y9ysmboj8yth/AABF0vI27sfywfL3Rjjg1H29a?dl=0

Can anyone explain me why these two merged documents are different ?

The Good.docx is result of merging, saved on server. TBS merge no errors reported. Word 2010 opens it with no error messages.

When I download Good.docx on my local disc, I got Corrupted.docx (download done with PHP script).

Both files have good merged content.

Thanks in advance.

Dako Simo.
By: Skrol29
Date: 2016-05-26
Time: 01:10

Re: download problem, example documents

Hi Dako,

This is common error when you begin with OpenTBS: your downloaded DOCX is corrupted because your PHP script send content to the ouput before the end of the merging. In your case they are two line-breaks in the top of the binary content. That's why Ms Word easily bypass the corruption.
You have two line-breaks probably because on of your PHP script ou sub-script has extra characters after the losing tag « ?> ». The best practice is to omit the last closing tag  « ?> » at the end of PHP scripts, PHP let them optionals exactly for such problems.

See: http://www.tinybutstrong.com/opentbs.php?doc#debug_error
By: dako
Date: 2016-05-26
Time: 10:51

Re: download problem, example documents

Hi Scrol29. Many thanks for your support !

What confuses me is that OpenTBS produces Good.docx on server.
Then, when I double click _that_  file, Word 2010 opens  it  with no errors (as I said I work with xampp, so easily can find that file).

After creating Good.docx I have another PHP script to download Good.docx from server to local disk.
Download done with no errors. I nameed downloaded file with Corrupted.docx.
Downloaded file has problem (as you said, two line-breaks in the top of the binary content).

As I am fresh in PHP, now I do not recognize which script to analyze ? Who made two line-breaks in the top of the binary content ?
PHP script that did OpenTBS merge job (but how Good.docx is good ?), or PHP script that did download (as it looks for me as true) ?.

At the end, is there are any way to donate OpenTBS ?

Best regards,
Dako Simo.
By: dako
Date: 2016-05-27
Time: 11:02

Re: download problem, example documents

Hi Skrol29.
Meanwhile I found solution. It may be usefull  to know for other faced with same problem.
I was convinced that OpenTBS  did merge correctly. I analyzed PHP script and found no extra outputs before or after  $TBS->Show(OPENTBS_FILE, $output_file_name);  command.
So I suspected on PHP script used to download file from server to my local disk. It was some common script we can find anywhere on Internet.  Main parts of script looks like:

$fsize = filesize($filename);  // $filename contains full path
// ctype for docx files:
$ctype="application/vnd.openxmlformats-officedocument.wordprocessingml.document";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
set_time_limit(0);
@readfile("$filename") or die("File not found.");

Using this script I download  docx file on my local disk. But I had problem to open it with Word 2010, reporting corrupted file.
Then,  I started to investigate on Internet problems with downloading especially docx files. I was surprised how it is frequent to get downloaded this type file with corrupted content. 
Among several suggestions my problem cures next lines:

set_time_limit(0);
ob_clean();
flush();
@readfile("$filename") or die("File not found.");

I only added  lines ob_clean();  and  flush();   and problem gone. Now I can download  and open docx  file with correct content. No change in script uset to do OpenTBS merge.

Best regards,

Dako Simo.
By: Skrol29
Date: 2016-05-28
Time: 23:37

Re: download problem, example documents

Ok thanks,

But I don't understand why ob_clean() + flush() can prevent to send extra content.