Tuesday, September 30, 2014

PHP, convert latex to pdf



To convert latex to pdf in PHP, first we need to download text live
 http://www.tug.org/texlive/acquire-netinstall.html
xelatex  executable from text live will be used in PHP 
1) In temporary directory, we put the text template as a tex file and run xelatex to convert tex to pdf.
then display pdf file.
Note;
ob_get_contentsReturn the contents of the output buffer
 tempnamCreate file with unique file name
sys_get_temp_dirReturns directory path used for temporary files
basenameReturns trailing name component of path

index.php
<?php
        ob_start();
        include 'latex_template.php';
        $outputData .= ob_get_contents();
        ob_end_clean();
        $texFile = tempnam(sys_get_temp_dir(), 'test');
        $base = basename($texFile);
       
        rename($texFile, $texFile.".tex");
        $texFile .= ".tex";
        file_put_contents($texFile, $outputData);
        chdir(dirname(realpath($texFile)));

        $console = shell_exec("xelatex {$base}");
        header("Content-type: application/pdf");
        $pdf =    dirname(realpath($texFile)).DIRECTORY_SEPARATOR.$base.".pdf";
        readfile($pdf);
?>

latex_template.php
\documentclass[12pt]{article}
\usepackage{lingmacros}
\usepackage{tree-dvips}
\begin{document}

\section*{Notes for My Paper}

Don't forget to include examples of topicalization.
They look like this:

{\small
\enumsentence{Topicalization from sentential subject:\\
\shortex{7}{a John$_i$ [a & kltukl & [el &
  {\bf l-}oltoir & er & ngii$_i$ & a Mary]]}
{ & {\bf R-}clear & {\sc comp} &
  {\bf IR}.{\sc 3s}-love   & P & him & }
{John, (it's) clear that Mary loves (him).}}
}

\subsection*{How to handle topicalization}

I'll just assume a tree structure like (\ex{1}).

{\small
\enumsentence{Structure of A$'$ Projections:\\ [2ex]
\begin{tabular}[t]{cccc}
    & \node{i}{CP}\\ [2ex]
    \node{ii}{Spec} &   &\node{iii}{C$'$}\\ [2ex]
        &\node{iv}{C} & & \node{v}{SAgrP}
\end{tabular}
\nodeconnect{i}{ii}
\nodeconnect{i}{iii}
\nodeconnect{iii}{iv}
\nodeconnect{iii}{v}
}
}

\subsection*{Mood}

Mood changes when there is a topic, as well as when
there is WH-movement.  \emph{Irrealis} is the mood when
there is a non-subject topic or WH-phrase in Comp.
\emph{Realis} is the mood when there is a subject topic
or WH-phrase.

\end{document}

  Video: PHP, convert latex to pdf

2 comments:

  1. Hi Lu. I have been trying to solve replicate your tutorial, but keep getting this error: "Error: Invalid XRef stream header"
    What is the cause of this issue ?

    ReplyDelete
    Replies
    1. Error: Invalid XRef stream header pdf.worker.js:232:5
      XRef_readXRef@resource://pdf.js/build/pdf.worker.js:3708:13
      XRef_parse@resource://pdf.js/build/pdf.worker.js:3296:23
      PDFDocument_setup@resource://pdf.js/build/pdf.worker.js:2469:7
      PDFDocument_parse@resource://pdf.js/build/pdf.worker.js:2350:7
      LocalPdfManager_ensure/<@resource://pdf.js/build/pdf.worker.js:1905:20
      LocalPdfManager_ensure@resource://pdf.js/build/pdf.worker.js:1900:1
      BasePdfManager_ensureDoc@resource://pdf.js/build/pdf.worker.js:1832:14
      loadDocument/</<@resource://pdf.js/build/pdf.worker.js:33604:11
      pdf.worker.js:233:1
      Warning: Unsupported feature "unknown" pdf.worker.js:224:5
      Warning: Unsupported feature "unknown" pdf.js:224:5
      Warning: Indexing all PDF objects pdf.worker.js:224:5
      Invalid or corrupted PDF file.
      PDF.js v1.1.469 (build: f06aa6a)
      Message: Invalid PDF structure

      Delete