Menu

Twips / CM or Inch

PhpRtf
2011-02-25
2013-04-30
  • Merritt Lee

    Merritt Lee - 2011-02-25

    HI Sigma,

    This is a great project!

    This is my first time using SourceForge and commenting on a project, so let me know if there's a better place to toss in my 2¢…

    I'm pretty sure that a simple way to let folks use PHPRtfLite in inches as well as cms would be to do global  find/replace for     TWIPS_IN_CM and replace it with something like TWIPS_IN_UNIT or TWIPS_PER_UNIT or whatever you want  and have two lines in lib/PHPRtfLite.php line 36:

    const TWIPS_IN_UNIT  = 567;  //sets global units to cm
    //const TWIPS_IN_UNIT = 1440; //sets global units to inch

    Maybe throw in a warning that all your examples (awesome btw), are in cm and changing global units to inches will muck up the lot.

    Just throwing this in because there are so many imperial measurements for paper, that is seems like a quick way to make this a more globally usable class.

    Thanks!
    -Merritt (:

     
  • Steffen Zeidler

    Steffen Zeidler - 2011-02-25

    Hi Merrit.

    Thanks for the praise.

    This a nice idea for supporting different units.
    I'll check, if it is not too late for adding it for the 1.1 version, because the release candiate version is about to be released this weekend, so we are not far from releasing stable version 1.1.

    There is a section called tracker, where you can add feature requests and also, if something is wrong, bug reports.

    Regards,
    sigmaz

     
  • Steffen Zeidler

    Steffen Zeidler - 2011-02-25

    Hi Merrit.

    I added the unit functionality. You can use a different unit by using:
    PHPRtfLite_Unit::setGlobalUnit(PHPRtfLite_Unit::UNIT_INCH);
    All widths and heights are then taken to be in inches.

    It will be released in v1.1-RC this weekend.

    Regards,
    Sigmaz

     
  • Merritt Lee

    Merritt Lee - 2011-02-26

    Hi Sigmaz!

    Great to hear about the paper support. RAD!

    Excited for the v1.1 this weekend! :)

    I probably missed how to do this, but i couldn't find a method to download the generated file, so i wrote a method for it in my copy of the beta lib/PHPRtfLite.php.

    If there is download capability already, just ignore the rest of this reply. ;)

    If "download file" is something that didn't exist yet in your code, please feel free to add this code snippet to your project:

    /**
         * download as file
         *
         * @param string $filename
         */
        public function downloadRtf($filename = 'simple.rtf')
        {
            $pathInfo = pathinfo($filename);

            if (empty($pathInfo)) {
                $filename .= '.rtf';
            }

            $file = sys_get_temp_dir() . '/' . $filename;
            $this->save($file);

            if (false !== strpos($_SERVER, 'MSIE 5.5')) {
                header('Content-Disposition: filename="' . $filename . '"');
            }
            else {
                header('Content-Disposition: inline; filename="' . $filename . '"');
            }
            header("Content-type: application/msword");

            $this->printTempFileContent($file);
        }

    Thanks!
    -Merritt (:

    Maybe useful for future dev:

    Something else i needed for a current project was a path to get a variable into the render() method. I needed a way to add a little rtf syntax to get MS word to stop printing an extra page after a table that filled the last page's printable area. Dropping in the "{\fs2 \f0 \cf0 \par}" right before the final "}" in render() works like a charm for this. Decided on the name=>value array because there might be some other variable that would come in handy as the code is being rendered.

    /**
         * download as file
         *
         * @param string $filename
         */
        public function downloadRtf($filename = 'simple.rtf', $renderParams = NULL)
        {
            $pathInfo = pathinfo($filename);

            if (empty($pathInfo)) {
                $filename .= '.rtf';
            }

            $file = sys_get_temp_dir() . '/' . $filename;
            $this->save($file, $renderParams);

            if (false !== strpos($_SERVER, 'MSIE 5.5')) {
                header('Content-Disposition: filename="' . $filename . '"');
            }
            else {
                header('Content-Disposition: inline; filename="' . $filename . '"');
            }
            header("Content-type: application/msword");

            $this->printTempFileContent($file);
        }

    public function save($file, $renderParams = NULL)
        {
            $this->_stream->open($file);
            $this->render($renderParams);
            $this->_stream->close();
        }
    protected function render($renderParams = NULL)
      { …..

    …..
           if ($renderParams)
           {
       $this->_stream->write('{\fs2 \f0 \cf0 \par}');
    }
            $this->_stream->write('}');
        }  // end render()

    Then called with:
    $rtf->downloadRtf('filename.rtf', array("hide_page_after_table"=>true));

    Waaaay OT:
    I don't think this bit of info is posted anywhere else, so i'm tossing it in here in case people are searching for a solution:

    For anyone who's trying to use html/css to generate MS Word docs and running into the blank page after a table that fills the printable area of the last page, add this code as the last line before your closing body tag:
    <p style="font-size:1.0pt; line-height: 0; padding: 0; margin: 0; border: 0;">&nbsp;</p>

     
  • Steffen Zeidler

    Steffen Zeidler - 2011-02-26

    Hi Merrit.

    Hope I get it right, a table is the last element rendered in the document and causes a page break.
    After rendering a table a \par is added by default. I'm not sure if this is the reason which caused your problem. With the upcoming release there is a method $table->preventEmptyParagraph() prevents rendering an empty paragraph after table. May be this solves the problem.

    Regards,
    SigmaZ

     
  • Merritt Lee

    Merritt Lee - 2011-02-28

    I think that MS Word adds a paragraph after table whether it exists in the rtf or not. My theory is that blank paragraph is how they allow folks to type text after a table. without a paragraph, there would be no place for the cursor.

    Rather convoluted, but the only solution i found anywhere was to force that last paragraph to have font size 1pt. so that it has almost no vertical height and thus squeezes onto the previous page.

    I'll totally test the $table->preventEmptyParagraph(), but if by chance it doesn't solve the blank page issue, you'll know about tossing in the tiny paragraph after tables. :)

     
  • Merritt Lee

    Merritt Lee - 2011-02-28

    Just tested the $table->preventEmptyParagraph() and i'm still pretty sure that the blank paragraph after a table is not dependent on the rtf code, rather the programs' need to allow cursor placement after a table. the 1pt font is a hack, but it does make that last paragraph nice and tiny. ;)

     
  • Steffen Zeidler

    Steffen Zeidler - 2011-03-02

    Hi Merrit,

    thanks for the notice. You are right, MS Word seems to add a line propably just for having the option to set the cursor right after the table. Nice hack of you. But this is nothing what I would add to the library's code - looks too much like a hack to me. What do you think?

    Regards

     
  • Merritt Lee

    Merritt Lee - 2011-04-06

    Yep, it's a total hack and i can see why it wouldn't fit in the code library. :)

     

Log in to post a comment.