From a0d5d78d2664f9e1abe68b2722a0d15c33955b97 Mon Sep 17 00:00:00 2001 From: Sashi20 Date: Tue, 25 Jun 2019 14:10:05 +0530 Subject: Enable link to download pdf --- latex.inc | 253 ++++++++++++++++++++++++------------------------ latex/Initial_body | 112 ++++++++++----------- latex/bck_latex_test.sh | 2 +- latex/latex_test.sh | 12 +-- latex/pdf_creator.sh | 9 +- run.inc | 13 ++- 6 files changed, 208 insertions(+), 193 deletions(-) diff --git a/latex.inc b/latex.inc index 422bb0c..ab606fe 100755 --- a/latex.inc +++ b/latex.inc @@ -1,16 +1,19 @@ fields('textbook_companion_preference'); - $query->condition('id', $preference_id); - $preference_q = $query->execute(); + $preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = :id", array( + ':id' => $preference_id + )); $preference_data = $preference_q->fetchObject(); if (!$preference_data) - { + { drupal_set_message('Invalid book specified.', 'error'); drupal_goto(''); - } + } //!$preference_data if ($preference_data->approval_status == 0) - { + { drupal_set_message('Book proposal is still in pending review.', 'error'); drupal_goto(''); - } + } //$preference_data->approval_status == 0 $book_filedata = $preference_data->book . $sep . $preference_data->author . $sep . $preference_data->isbn . $sep . $preference_data->publisher . $sep . $preference_data->edition . $sep . $preference_data->year . $eol; /* regenerate book if full book selected */ if ($full_book) del_book_pdf($preference_data->id); /* check if book already generated */ + //var_dump(file_exists($dir_path . "book_" . $preference_data->id . ".pdf"));die; if (file_exists($dir_path . "book_" . $preference_data->id . ".pdf")) - { - /* download zip file */ + { + /* download PDF file */ + ob_clean(); + header("Pragma: public"); + header("Expires: 0"); + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + header("Cache-Control: public"); + header("Content-Description: File Transfer"); header('Content-Type: application/pdf'); header('Content-disposition: attachment; filename="' . $preference_data->book . '_' . $preference_data->author . '.pdf"'); header('Content-Length: ' . filesize($dir_path . "book_" . $preference_data->id . ".pdf")); - header("Content-Transfer-Encoding: binary"); - header('Expires: 0'); - header('Pragma: no-cache'); - ob_end_flush(); - ob_clean(); - flush(); + //header("Content-Transfer-Encoding: binary"); + //header('Expires: 0'); + //header('Pragma: no-cache'); + //ob_end_flush(); + //ob_clean(); + //flush(); readfile($dir_path . "book_" . $preference_data->id . ".pdf"); return; - } - /*$proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = %d", $preference_data->proposal_id); - $proposal_data = db_fetch_object($proposal_q);*/ - $query = db_select('textbook_companion_proposal'); - $query->fields('textbook_companion_proposal'); - $query->condition('id', $preference_data->proposal_id); - $proposal_q = $query->execute(); + } //file_exists($dir_path . "book_" . $preference_data->id . ".pdf") + $proposal_q = db_query("SELECT * FROM {textbook_companion_proposal} WHERE id = :id", array( + ':id' => $preference_data->proposal_id + )); $proposal_data = $proposal_q->fetchObject(); if (!$proposal_data) - { + { drupal_set_message('Could not fetch contributors information for the book specified.', 'error'); - } + } //!$proposal_data $contributor_filedata .= $proposal_data->full_name . $sep . $proposal_data->course . $sep . $proposal_data->branch . $sep . $proposal_data->university . $sep . $proposal_data->faculty . $sep . $proposal_data->reviewer . $eol; - /*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d ORDER BY number DESC", $preference_data->id);*/ - $query = db_select('textbook_companion_chapter'); - $query->fields('textbook_companion_chapter'); - $query->condition('preference_id', $preference_data->id); - $query->orderBy('number', 'DESC'); - $chapter_q = $query->execute(); + $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = :id ORDER BY number DESC", array( + ':id' => $preference_data->id + )); while ($chapter_data = $chapter_q->fetchObject()) - { + { if ($full_book) - { - /*$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d ORDER BY number DESC", $chapter_data->id);*/ - $query = db_select('textbook_companion_example'); - $query->fields('textbook_companion_example'); - $query->condition('chapter_id', $chapter_data->id); - $query->orderBy('number', 'DESC'); - $example_q = $query->execute(); - } + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = :id ORDER BY number DESC", array( + ':id' => $chapter_data->id + )); else - { - /*$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1 ORDER BY number DESC", $chapter_data->id);*/ - $query = db_select('textbook_companion_example'); - $query->fields('textbook_companion_example'); - $query->condition('chapter_id', $chapter_data->id); - $query->condition('approval_status', 1); - $query->orderBy('number', 'DESC'); - $example_q = $query->execute(); - } + $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = :id AND approval_status = 1 ORDER BY number DESC", array( + ':id' => $chapter_data->id + )); while ($example_data = $example_q->fetchObject()) - { - /*$example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_data->id);*/ - $query = db_select('textbook_companion_example_files'); - $query->fields('textbook_companion_example_files'); - $query->condition('example_id', $example_data->id); - $example_files_q = $query->execute(); + { + $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = :id", array( + ':id' => $example_data->id + )); while ($example_files_data = $example_files_q->fetchObject()) - { + { $latex_filedata .= $chapter_data->number . $sep; $latex_filedata .= $chapter_data->name . $sep; $latex_filedata .= $example_data->number . $sep; $latex_filedata .= $example_data->caption . $sep; $latex_filedata .= $example_files_data->filename . $sep; - $latex_filedata .= $example_files_data->filepath . $sep; + $latex_filedata .= $preference_data->directory_name . '/' . $example_files_data->filepath . $sep; $latex_filedata .= $example_files_data->filetype . $sep; $latex_filedata .= $sep; $latex_filedata .= $example_files_data->id; $latex_filedata .= $eol; - } - /*$dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_data->id);*/ - $query = db_select('textbook_companion_example_dependency'); - $query->fields('textbook_companion_example_dependency'); - $query->condition('example_id', $example_data->id); - $dependency_files_q = $query->execute(); + } //$example_files_data = $example_files_q->fetchObject() + /*$dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = :id", array( + ':id' => $example_data->id + )); while ($dependency_files_data = $dependency_files_q->fetchObject()) - { - /*$dependency_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $dependency_files_data->dependency_id);*/ - $query = db_select('textbook_companion_dependency_files'); - $query->fields('textbook_companion_dependency_files'); - $query->condition('id', $dependency_files_data->dependency_id); - $query->range(0, 1); - $dependency_q = $query->execute(); + { + $dependency_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = :id LIMIT 1", array( + ':id' => $dependency_files_data->dependency_id + )); if ($dependency_data = $dependency_q->fetchObject()) - { + { $latex_filedata .= $chapter_data->number . $sep; $latex_filedata .= $chapter_data->name . $sep; $latex_filedata .= $example_data->number . $sep; @@ -142,34 +125,31 @@ function _latex_generate_files($preference_id, $full_book = FALSE) $latex_filedata .= $dependency_data->id; $latex_filedata .= $eol; $depedency_list[$dependency_data->id] = "D"; - } - } - } - } - foreach ($depedency_list as $row => $data) - { - /*$dependency_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $row);*/ - $query = db_select('textbook_companion_dependency_files'); - $query->fields('textbook_companion_dependency_files'); - $query->condition('id', $row); - $query->range(0, 1); - $dependency_q = $query->execute(); + } //$dependency_data = $dependency_q->fetchObject() + } */ //$dependency_files_data = $dependency_files_q->fetchObject() + } //$example_data = $example_q->fetchObject() + } //$chapter_data = $chapter_q->fetchObject() + /*foreach ($depedency_list as $row => $data) + { + $dependency_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = :id LIMIT 1", array( + ':id' => $row + )); if ($dependency_data = $dependency_q->fetchObject()) - { + { $latex_dep_filedata .= $dependency_data->filename . $sep; $latex_dep_filedata .= $dependency_data->filepath . $sep; $latex_dep_filedata .= $dependency_data->caption . $sep; $latex_dep_filedata .= $dependency_data->id; $latex_dep_filedata .= $eol; - } - } + } //$dependency_data = $dependency_q->fetchObject() + }*/ //$depedency_list as $row => $data /**************************** WRITE TO FILES ********************************/ $download_filename = $preference_data->book . "_" . $preference_data->author; $book_filename = "tmp_" . $preference_data->id . "_book.txt"; $contributor_filename = "tmp_" . $preference_data->id . "_contributor.txt"; $latex_filename = "tmp_" . $preference_data->id . "_data.txt"; - $latex_dep_filename = "tmp_" . $preference_data->id . "_dep_data.txt"; - $pdf_filename = "book_" . $preference_data->id . ".pdf"; + /*$latex_dep_filename = "tmp_" . $preference_data->id . "_dep_data.txt";*/ + $pdf_filename = "book_" . $preference_data->directory_name . ".pdf"; // $book_filedata = str_replace("&", "\&", $book_filedata); $fb = fopen($dir_path . $book_filename, 'w'); fwrite($fb, $book_filedata); @@ -181,56 +161,81 @@ function _latex_generate_files($preference_id, $full_book = FALSE) $fl = fopen($dir_path . $latex_filename, 'w'); fwrite($fl, $latex_filedata); fclose($fl); - $fd = fopen($dir_path . $latex_dep_filename, 'w'); + /*$fd = fopen($dir_path . $latex_dep_filename, 'w'); fwrite($fd, $latex_dep_filedata); - fclose($fd); - if (_latex_run_script($book_filename, $contributor_filename, $latex_filename, $latex_dep_filename, $pdf_filename)) - { - /* download zip file */ - header('Content-Type: application/pdf'); - header('Content-disposition: attachment; filename="' . $preference_data->book . '_' . $preference_data->author . '.pdf"'); - header('Content-Length: ' . filesize($dir_path . $pdf_filename)); - header("Content-Transfer-Encoding: binary"); - header('Expires: 0'); - header('Pragma: no-cache'); - ob_end_flush(); - ob_clean(); - flush(); - readfile($dir_path . $pdf_filename); - } + fclose($fd);*/ + //var_dump($dir_path);die; + if (_latex_run_script($book_filename, $contributor_filename, $latex_filename, $pdf_filename)) + { + /* download PDF file */ + // var_dump($dir_path . $pdf_filename);die; + if (filesize($dir_path . $pdf_filename) == TRUE) + { + //ob_clean(); + header("Pragma: public"); + header("Expires: 0"); + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + header("Cache-Control: public"); + header("Content-Description: File Transfer"); + header('Content-Type: application/pdf'); + header('Content-disposition: attachment; filename="' . $preference_data->book . '_' . $preference_data->author . '.pdf"'); + header('Content-Length: ' . filesize($dir_path . $pdf_filename)); + header("Content-Transfer-Encoding: binary"); + header('Expires: 0'); + header('Pragma: no-cache'); + @readfile($dir_path . $pdf_filename); + ob_end_flush(); + ob_clean(); + flush(); + } //filesize($dir_path . $pdf_filename) == TRUE + else + { + drupal_set_message("Error occurred generating the PDF version of the Book.", 'error'); + drupal_goto('textbook-companion/textbook-run/' . $preference_data->id); + } + } //_latex_run_script($book_filename, $contributor_filename, $latex_filename, $latex_dep_filename, $pdf_filename) else - { + { drupal_set_message("Error occurred when generating the PDF version of the Book.", 'error'); - } + } /*********************** DELETING TEMPORARY FILES ***************************/ /* regenerate book if full book selected */ if ($full_book) del_book_pdf($preference_data->id); - } +} function _latex_copy_script_file() - { - exec("cp ./" . drupal_get_path('module', 'textbook_companion') . "/latex/* ./uploads/latex"); - exec("chmod u+x ./uploads/latex/*.sh"); - } -function _latex_run_script($book_filename, $contributor_filename, $latex_filename, $latex_dep_filename, $pdf_filename) - { +{ + exec("cp ./" . drupal_get_path('module', 'textbook_companion') . "/latex/* ./r_uploads/tbc_uploads/latex"); + exec("chmod u+x ./r_uploads/tbc_uploads/latex/*.sh"); +} +function _latex_run_script($book_filename, $contributor_filename, $latex_filename, $pdf_filename) +{ + // var_dump($book_filename . $contributor_filename . $latex_filename . $pdf_filename);die; $root_path = textbook_companion_path(); $ret = 0; - chdir("uploads"); + chdir("r_uploads/tbc_uploads"); chdir("latex"); - $sh_command = "./pdf_creator.sh " . $book_filename . " " . $contributor_filename . " " . $latex_filename . " " . $latex_dep_filename; + //var_dump($book_filename . " " . $contributor_filename . " " . $latex_filename . " " . $latex_dep_filename);die; + $sh_command = "./pdf_creator.sh " . $book_filename . " " . $contributor_filename . " " . $latex_filename . " " . $pdf_filename; exec($sh_command); - exec("mv TEX_final.pdf " . $pdf_filename); + //var_dump($sh_command);die; + //exec("mv TEX_final.pdf " , $pdf_filename); + // rename('TEX_final.pdf', $pdf_filename); + //var_dump($pdf_filename);die; if ($ret == 0) + { return TRUE; + } //$ret == 0 else + { return FALSE; - } + } +} function textbook_companion_delete_book() - { +{ $book_id = arg(2); del_book_pdf($book_id); drupal_set_message(t('Book schedule for regeneration.'), 'status'); drupal_goto('code_approval/bulk'); return; - } +} diff --git a/latex/Initial_body b/latex/Initial_body index c8d3576..c0a6b63 100755 --- a/latex/Initial_body +++ b/latex/Initial_body @@ -1,58 +1,58 @@ \nonstopmode -\documentclass[12pt]{report} -\usepackage{hyperref} -\hypersetup{colorlinks=true,linkcolor=blue} -\usepackage{theorem,graphicx} -\usepackage{listings,alltt} -\bibliographystyle{plain} - - -\lstset{ %configuring the display of scilab codes - tabsize=4, - language=scilab, - basicstyle=\ttfamily, - aboveskip={1\baselineskip}, - showstringspaces=false, - breaklines=true, - showspaces=false, - numbers=left, - numberstyle=\small, - stringstyle=\normalfont, - keywordstyle=\color{red}, - emph={clc, all, gca}, - emphstyle=\color{red}, - commentstyle=\color{blue}\normalfont} - - -% code environment -{\theorembodyfont{\rmfamily} \newtheorem{codemass}{Scilab code}[chapter]} -\newenvironment{code}% -{\begin{codemass}}{\hrule \end{codemass}} - -{\theorembodyfont{\rmfamily} \newtheorem{accmass}{Acc}[chapter]} -\newenvironment{acc-code}% -{\begin{accmass}}{\end{accmass}} - - -% create listing for code - -\newcommand\tcaption[1] - {\addcontentsline{cod}{section}{\protect\numberline {\thecodemass}#1}} -\makeatletter \newcommand\listofcode - {\chapter*{List of Scilab Codes\markboth% - {\bf List of Scilab Codes}{}}% -\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{5em}}% -\addcontentsline{toc}{chapter}{\protect\numberline{List of Scilab Codes}} -\@starttoc{cod}} -\newcommand\l@matlab[3] - {#1 \par\noindent#2, #3 \par} -\renewcommand\@pnumwidth{2.1em} -%\makeatother - -\makeatletter -\def\curlable#1{\def\thecodemass{#1}\def\@currentlabel{#1}} -\makeatother - -\newcommand{\coderef}[1]{Exa~\ref{#1}} -\newcommand{\figref}[1]{Fig.~\ref{#1}} +\documentclass[12pt]{report} +\usepackage{hyperref} +\hypersetup{colorlinks=true,linkcolor=blue} +\usepackage{theorem,graphicx} +\usepackage{listings,alltt} +\bibliographystyle{plain} + + +\lstset{ %configuring the display of scilab codes + tabsize=4, + language=R, + basicstyle=\ttfamily, + aboveskip={1\baselineskip}, + showstringspaces=false, + breaklines=true, + showspaces=false, + numbers=left, + numberstyle=\small, + stringstyle=\normalfont, + keywordstyle=\color{red}, + emph={clc, all, gca}, + emphstyle=\color{red}, + commentstyle=\color{blue}\normalfont} + + +% code environment +{\theorembodyfont{\rmfamily} \newtheorem{codemass}{Scilab code}[chapter]} +\newenvironment{code}% +{\begin{codemass}}{\hrule \end{codemass}} + +{\theorembodyfont{\rmfamily} \newtheorem{accmass}{Acc}[chapter]} +\newenvironment{acc-code}% +{\begin{accmass}}{\end{accmass}} + + +% create listing for code + +\newcommand\tcaption[1] + {\addcontentsline{cod}{section}{\protect\numberline {\thecodemass}#1}} +\makeatletter \newcommand\listofcode + {\chapter*{List of Scilab Codes\markboth% + {\bf List of Scilab Codes}{}}% +\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{5em}}% +\addcontentsline{toc}{chapter}{\protect\numberline{List of Scilab Codes}} +\@starttoc{cod}} +\newcommand\l@matlab[3] + {#1 \par\noindent#2, #3 \par} +\renewcommand\@pnumwidth{2.1em} +%\makeatother + +\makeatletter +\def\curlable#1{\def\thecodemass{#1}\def\@currentlabel{#1}} +\makeatother + +\newcommand{\coderef}[1]{Exa~\ref{#1}} +\newcommand{\figref}[1]{Fig.~\ref{#1}} diff --git a/latex/bck_latex_test.sh b/latex/bck_latex_test.sh index 6cfe29a..b7c0513 100755 --- a/latex/bck_latex_test.sh +++ b/latex/bck_latex_test.sh @@ -134,7 +134,7 @@ fi j=$col1 done < database_sort -rm Figure_files +#rm Figure_files if [ -s $Dep_dat ] diff --git a/latex/latex_test.sh b/latex/latex_test.sh index 5d67cb5..ebd71eb 100755 --- a/latex/latex_test.sh +++ b/latex/latex_test.sh @@ -1,9 +1,9 @@ #!/bin/bash clear -Bk_details=$1 -Contrib_details=$2 +CBk_details=$1 +CContrib_details=$2 Data_all=$3 -Dep_dat=$4 +#Dep_dat=$4 CURDIR=$PWD; @@ -24,8 +24,8 @@ fi IFS_old="$IFS" -IFS=# read col1 col2 col3 col4 col5 col6 col7 col8 < $Contrib_details; -IFS=# read colB1 colB2 colB3 colB4 colB5 colB6 colB7 < $Bk_details; +IFS=# read col1 col2 col3 col4 col5 col6 col7 col8 < "$CContrib_details"; +IFS=# read colB1 colB2 colB3 colB4 colB5 colB6 colB7 < "$CBk_details"; col1=${col1/&/\\&}; col2=${col2/&/\\&}; col3=${col3/&/\\&}; @@ -152,7 +152,7 @@ fi j=$col1 done < database_sort -rm Figure_files +#rm Figure_files if [ -s $Dep_dat ] diff --git a/latex/pdf_creator.sh b/latex/pdf_creator.sh index 2e2cbe0..afc2ce0 100755 --- a/latex/pdf_creator.sh +++ b/latex/pdf_creator.sh @@ -1,4 +1,4 @@ -$ cat changedir.sh +#$ cat changedir.sh #!/bin/bash @@ -6,14 +6,13 @@ if [ -f TEX_final.tex ] then rm TEX_final.* rm database_sort - rm database_sort.csv rm TEX fi ./latex_test.sh $1 $2 $3 $4 pdflatex TEX_final.tex > log.txt pdflatex TEX_final.tex >> log1.txt -pdflatex TEX_final.tex >> log1.txt -pdflatex TEX_final.tex >> log1.txt - +#pdflatex TEX_final.tex >> log1.txt +#pdflatex TEX_final.tex >> log1.txt +mv -v TEX_final.pdf $4 >> log.txt rm log1.txt diff --git a/run.inc b/run.inc index 6ca90b2..ee31a2b 100755 --- a/run.inc +++ b/run.inc @@ -51,6 +51,10 @@ function textbook_companion_run_form($form, &$form_state) '#type' => 'item', '#markup' => '