Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception is thrown when a string containing 'less than' sign (<) is passed from controller to blade . #3

Closed
dhruva81 opened this issue Aug 24, 2021 · 5 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@dhruva81
Copy link

Hi @ismaelw

The package is throwing exception when we pass a string containing 'less than ' character (<) from controller to blade tex file.

Kindly check following controller and blade files. These are not working and exception is thrown.

Controller

     public function laratex()
      {
            $data = "$ (a < b) $";

            return (new LaraTeX('tex'))->with([
                        'data' => $data,
                  ])->download( 'test.pdf');
      }

Blade


\documentclass[a4paper,9pt,landscape]{article}

\usepackage{adjustbox}
\usepackage[english]{babel}
\usepackage[scaled=.92]{helvet}
\usepackage{fancyhdr}
\usepackage[svgnames,table]{xcolor}
\usepackage[a4paper,inner=1.5cm,outer=1.5cm,top=1cm,bottom=1cm,bindingoffset=0cm]{geometry}
\usepackage{blindtext}
\geometry{textwidth=\paperwidth, textheight=\paperheight, noheadfoot, nomarginpar}

\renewcommand{\familydefault}{\sfdefault}

\pagestyle{fancy}
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\fancyfoot[LE,RO]{\thepage}

\fancyfoot[C]{\fontsize{8pt}{8pt}\selectfont Above document is auto-generated.}
\renewcommand{\footrulewidth}{0.2pt}


\begin{document}
 
      {{  $data }}

\end{document}

Exception

Ismaelw\LaraTeX\LaratexException


Misplaced alignment tab character &. l.27 $a & lt; b$ ? ! Emergency stop. l.27 $a & lt; b$ ! ==> Fatal error occurred, no output PDF file produced! 

Ismaelw\LaraTeX\LaraTeX::parseError

image

But if I use 'less than' symbol (<) directly in blade, then it is working fine and PDF is generating.

For example - below code is working fine

Controller

  public function raw()
  {
           return (new LaraTeX('tex'))->download( 'test.pdf');
  }

Blade


\documentclass[a4paper,9pt,landscape]{article}

\usepackage{adjustbox}
\usepackage[english]{babel}
\usepackage[scaled=.92]{helvet}
\usepackage{fancyhdr}
\usepackage[svgnames,table]{xcolor}
\usepackage[a4paper,inner=1.5cm,outer=1.5cm,top=1cm,bottom=1cm,bindingoffset=0cm]{geometry}
\usepackage{blindtext}
\geometry{textwidth=\paperwidth, textheight=\paperheight, noheadfoot, nomarginpar}

\renewcommand{\familydefault}{\sfdefault}

\pagestyle{fancy}
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\fancyfoot[LE,RO]{\thepage}

\fancyfoot[C]{\fontsize{8pt}{8pt}\selectfont Above document is auto-generated.}
\renewcommand{\footrulewidth}{0.2pt}


\begin{document}
 
   $ a < b $

\end{document}

Kindly help, how can I pass string containing less than sign (<) from controller to blade and render them in PDF.

Thank you

@dhruva81 dhruva81 changed the title Exception is thrown when less than sign is used. Exception is thrown when a string containing 'less than' sign (<) is passed from controller to blade . Aug 24, 2021
@ismaelw ismaelw self-assigned this Aug 24, 2021
@ismaelw ismaelw added the bug Something isn't working label Aug 24, 2021
@ismaelw
Copy link
Owner

ismaelw commented Aug 24, 2021

Hi @dhruva81

Thank you so much for your message and sharing your issue.
Please give me some time so I can look into it.

If you already have an idea yourself on where the issue could be I am more than happy to hear.

Kind regards,
Ismael

@ismaelw
Copy link
Owner

ismaelw commented Aug 24, 2021

Hi @dhruva81

I created a test environment myself and tried everything the way you did.
In my case I had another issue that is basically the same like yours but at another location.

As far as I can reproduce and analyze it, the issue is with how Laravel Blade works. In Blade Templating {{ }} statements are automatically converted with the PHP Function htmlspecialchars() to prevent XSS attacks. So your special characters get converted like this: & = &amp; and < = &lt;. This makes pdflatex fail when rendering.

To fix that issue you would need laravels builtin {!! !!} statement to get unescaped data written to your tex template.

So in your case it would look like this:

\documentclass[a4paper,9pt,landscape]{article}

\usepackage{adjustbox}
\usepackage[english]{babel}
\usepackage[scaled=.92]{helvet}
\usepackage{fancyhdr}
\usepackage[svgnames,table]{xcolor}
\usepackage[a4paper,inner=1.5cm,outer=1.5cm,top=1cm,bottom=1cm,bindingoffset=0cm]{geometry}
\usepackage{blindtext}
\geometry{textwidth=\paperwidth, textheight=\paperheight, noheadfoot, nomarginpar}

\renewcommand{\familydefault}{\sfdefault}

\pagestyle{fancy}
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\fancyfoot[LE,RO]{\thepage}

\fancyfoot[C]{\fontsize{8pt}{8pt}\selectfont Above document is auto-generated.}
\renewcommand{\footrulewidth}{0.2pt}


\begin{document}
 
      {!! $data !!}

\end{document}

Please check this solution in your environment and please also tell me if it fixes your issue :)

Kind regards,
Ismael

@ismaelw ismaelw added help wanted Extra attention is needed and removed bug Something isn't working labels Aug 24, 2021
@ismaelw
Copy link
Owner

ismaelw commented Aug 25, 2021

Hi @dhruva81

As soon as you have been able to check the solution above, I am happy to get some feedback.
If you need any further assistance do not hesitate to contact me :)

Kind regards,
Ismael

@dhruva81
Copy link
Author

Hi @ismaelw

Thank you for your reply.

I have tried using solution provided by you and it is working fine :)

\begin{document}
 
      {!! $data !!}

\end{document}

Thank you very much 👍 💯

@ismaelw
Copy link
Owner

ismaelw commented Aug 26, 2021

Hi @dhruva81

Thank you so much for your reply.
I will mark this issue as closed.

And again, if I can help you with something else please cobtact me 👍🏼

@ismaelw ismaelw closed this as completed Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants