I have a model attribute that saves user's HTML Textarea input. Because when the user hit enter in the Textarea, the form submitted, I have written a small Javascript code to input the "\n" automatically, rendering line break.
When I try to export this attribute to PDF using ReportLab, I receive non-printable characters that looks like 2 black rectangles. When I try to copy them to textpad, it becomes "n".
here is the code from the model
class Package(models.Model):
content = models.TextField()
here is the JavaScript and Html for the specific field:
<form action="" role="form" method="post">{% csrf_token %}
<textarea class="form-control" autofocus name="content">{{content}}</textarea>
<br/>
</form>
<script type="text/javascript">
$('textarea').keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
var s = $(this).val();
$(this).val(s+"\n");
}
});
</script>
This is the Reportlab code from views.py:
from reportlab.pdfgen import canvas
from reportlab.platypus import Spacer
from reportlab.lib.pagesizes import letter
def create(pdf):
packet = StringIO.StringIO()
can = canvas.Canvas(packet, pagesize=letter)
p_content = str(package.content)
textobject = can.beginText(0.5*inch, 4.75*inch)
for line in p_content:
textobject.textOut(line)
if line=="\n":
textobject.textLines(p_content)
can.save()
Please help. I cannot seem to figure this out.
Many thanks.
I have received outside help so I will just post it here for anyone who is interested.
The problem is this line of codes:
The think is i PRINT out before I do a check of the escape character. The correct code should be