question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to generate editable fields in Pylatex?

See original GitHub issue

Hi everyone, I’m looking foward how to inser a editable empty fields in the final pdf. In TexStudio I got this simple example of using forms: from this link `\documentclass[10pt]{article}

\usepackage{hyperref}

\begin{document} \begin{Form} \noindent \TextField[name=multilinetextbox, multiline=true, width=\linewidth,height=1in]{} \end{Form} \end{document}`

I look at PyLatex documentation, and I didn’t found something. Any help?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
JelteFcommented, Sep 11, 2017

UnsafeCommand might be nice because you can use backslashes, but be sure not to use UnsafeCommand with user controlled input. Or escape the user input using utils.escape. Otherwise you are vulnerable to (remote) code execution. You should also be able to use Command(‘linewidth’) to not have to use UnsafeCommand.

On Mon, Sep 11, 2017, 19:03 Scott Werner notifications@github.com wrote:

You need to create an Environment for the Form and an UnsafeCommand for the TextField. Check out https://jeltef.github.io/PyLaTeX/current/examples/own_commands_ex.html.

from pylatex import Document, Package, UnsafeCommandfrom pylatex.base_classes import Environment, Options

class FormEnvironment(Environment): “”“\begin{Form} … \end{Form}”“” _latex_name = ‘Form’ packages = [Package(‘hyperref’)] escape = False content_separator = ‘\n’

doc = Document() with doc.create(FormEnvironment()): # \TextField[name=multilinetextbox,multiline=true,width=\linewidth,height=1in]{} input_field = UnsafeCommand( ‘TextField’, arguments=[‘’], options=Options( name=‘multilinetextbox’, multiline=‘true’, width=r’\linewidth’, height=‘1in’) ) doc.append(input_field)

doc.generate_pdf(‘textfield_test’, compiler=‘pdflatex’, clean_tex=False)

Code above generated the following multiline input pdf: textfield_test.pdf https://github.com/JelteF/PyLaTeX/files/1293366/textfield_test.pdf

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/JelteF/PyLaTeX/issues/192#issuecomment-328593586, or mute the thread https://github.com/notifications/unsubscribe-auth/ABG8JtDZa0cSVn7UzBUzNHMbHPRVej3_ks5shWfqgaJpZM4PSYGJ .

1reaction
JelteFcommented, Nov 28, 2017

Good that you got it working. Only using UnsafeCommand command can most likely be improved indeed. Some tips:

  • You should be able to replace begin and end Form with an environment (because that’s what it is made for).
  • Be sure to use the utils.escape function on all user supplied text when passing it to UnsafeCommand, otherwise you open yourself up to remote shell command execution.
  • BaseCommand is probably useful as well for you. So you can make a custom class for some of your often used commands.
  • In general try to make classes for all your latex commands that you use often. That way you can make a more python like api for them. It’s fine to use UnsafeCommand inside some of your custom classes. If you do this it would be great if you could make a pull request with these classes, so other people can make pdf forms easily as well.

On Tue, Nov 28, 2017, 08:34 Isaque Daniel notifications@github.com wrote:

Hi @JelteF https://github.com/jeltef thanks for your help!! I got a solution using the “UnsafeCommand” in a little bit “ugly” way, but it works. In fact I pass all may code developed in latex interpreter line by line using UnsafeCommand. And it work perfect.

I will study better the Environment to build a class like the example of @scottwernervt https://github.com/scottwernervt specific to my problem (at same time learn more about Pylatex, it’s a incredible package).

` from pylatex import Document, NewPage, UnsafeCommand, Package, Section from pylatex.base_classes import Options

def generate_unique(): geometry_options = { “head”: “30pt”, “margin”: “0.3in”, “top”: “0.2in”, “bottom”: “0.3in”, “includeheadfoot”: True } doc = Document(geometry_options=geometry_options)

doc.packages.append(Package(‘hyperref’))

doc.append(Section(“Section to write”)) doc.append(UnsafeCommand(command=“newdimen\longline”)) doc.append(UnsafeCommand(command=“longline=\textwidth\advance\longline-4cm”)) doc.append(UnsafeCommand(command=“def\LayoutTextField#1#2”, extra_arguments=r’#2’)) doc.append(UnsafeCommand(command=“def\lbl#1”, extra_arguments=r’\hbox to 4cm{#1\dotfill\strut}‘)) doc.append(UnsafeCommand(command=“def\labelline#1#2”, extra_arguments=r’\lbl{#1}\vbox{\hbox{\TextField[name=#1,width=#2]{\null}}\kern2pt\hrule}‘)) doc.append( UnsafeCommand(command=“def\q#1”, extra_arguments=r’\hbox to \hsize{\labelline{#1}{\longline}}\vskip1.4ex’)) doc.append(UnsafeCommand(command=“begin”, extra_arguments=r’Form’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 1’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 2’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 3’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 4’)) doc.append(UnsafeCommand(command=“end”, extra_arguments=r’Form’))

doc.append(UnsafeCommand(command=“begin”, extra_arguments=r’flushleft’)) doc.append(“Some text:”) doc.append(UnsafeCommand(command=“end”, extra_arguments=r’flushleft’))

doc.append(UnsafeCommand(command=“begin”, extra_arguments=r’Form’)) doc.append(UnsafeCommand(command=“noindent”)) doc.append(UnsafeCommand(command=“TextField”, options=Options(name=‘multilinetextbox’, multiline=‘true’, width=‘1\linewidth’, height=‘1in’), extra_arguments=r’ ‘)) doc.append(UnsafeCommand(command=“end”, extra_arguments=r’Form’)) doc.append(“\n”)

doc.append(UnsafeCommand(command=“newdimen\longline”)) doc.append(UnsafeCommand(command=“longline=\textwidth\advance\longline-4cm”)) doc.append(UnsafeCommand(command=“def\LayoutTextField#1#2”, extra_arguments=r’#2’)) doc.append(UnsafeCommand(command=“def\lbl#1”, extra_arguments=r’\hbox to 4cm{#1\dotfill\strut}‘)) doc.append(UnsafeCommand(command=“def\labelline#1#2”, extra_arguments=r’\lbl{#1}\vbox{\hbox{\TextField[name=#1,width=#2]{\null}}\kern2pt\hrule}‘)) doc.append( UnsafeCommand(command=“def\q#1”, extra_arguments=r’\hbox to \hsize{\labelline{#1}{\longline}}\vskip1.4ex’))

doc.append(UnsafeCommand(command=“begin”, extra_arguments=r’Form’)) doc.append(“\n”) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 5’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 6’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 7’)) doc.append(UnsafeCommand(command=“q”, extra_arguments=r’Field 8’)) doc.append(UnsafeCommand(command=“end”, extra_arguments=r’Form’))

doc.append(NewPage())

doc.generate_pdf(“text_fields_report”, compiler=“pdflatex”, clean=True, clean_tex=False)

generate_unique()` the image of pdf (printscreen)

[image: Image of pdf] https://user-images.githubusercontent.com/5652407/33296460-fa0c42be-d3c2-11e7-852a-f5c4b05dfb18.png the pdf with the editable fields

text_fields_report.pdf https://github.com/JelteF/PyLaTeX/files/1508099/text_fields_report.pdf

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/JelteF/PyLaTeX/issues/192#issuecomment-347374775, or mute the thread https://github.com/notifications/unsubscribe-auth/ABG8JuQvfrw-wxQWNEhYSCOKLd5sStO3ks5s61T8gaJpZM4PSYGJ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create an editable field in a pdf using python latex? - TeX
import pylatex as pl class Form(pl.base_classes.Environment): """A class to wrap ... Document() with doc.create(Form()): doc.append(pl.
Read more >
Basic example — PyLaTeX 1.3.2 documentation - GitHub Pages
This example shows basic document generation functionality. fill_document (doc)[source]¶. Add a section, a subsection and some text to the document ...
Read more >
Pylatex module in python - GeeksforGeeks
Example 1: In this example we form a simple latex in order to from latex ... To create a document import document class...
Read more >
How to Create Fillable PDF Form for Free using Latex - YouTube
How to Create Fillable PDF Form for Free using LatexIn this video you will learn how to make a fillable pdf form document...
Read more >
Import custom latex script into pylatex document - Stack Overflow
I see PyLaTeX has a predefined syntax to generate LaTeX documents and then export them, but I want to simply load a LaTeX...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found