Reportlab - Truncating text after specific width while writing on canvas - python-3.x

I am working on a project which has a PDF editor that we have built in angular. User can drag and drop dynamic fields (user defined fields) on the PDF. These fields when are filled from a user define form, the corresponding PDF (that user has edited) gets generated in the backend. We are using Python 3.7 and Reportlab to edit and write dynamic data to the PDF.
The length of the value dynamic fields is not fixed. It can be more than the field variable's length. On PDF editor, the user decides max width of the generated dynamic text. We want to write the text till the specified width only and truncate rest of the text. For example, first_name is the variable dropped on the PDF editor. It's max width is set to 10px. If the value of the variable is "Wolfeschlegelsteinhausenbergerdorff", the text should be written till 10 px and should get truncated after that.
So far, we have managed to write the full text (irrespective of width specified). Following is the code that we are using.
........
paragraph_style.textColor = HexColor(pdf_element["font_color"])
paragraph_style.fontSize = pdf_element["font_size"]
paragraph = Paragraph(str(output_text_value), style=paragraph_style)
paragraph.drawOn(can,location_x, location_y)
........
Above code writes full text on the PDF. However, we need a way to somehow truncate the text after specified width.
Any help is greatly appreciated.

Instead of directly drawing paragraph on the canvas, use frame. Add the paragraph inside frame, Apply truncate mode to the frame and then draw frame on canvas. Your code should look like following:
frame = KeepInFrame(min_width, min_height, [paragraph], mode='truncate')
frame.width = float(min_.replace("px",""))
frame.drawOn(can, location_x, location_y)
Hope it helps. Thanks

Related

How to reduce the length of the databand in stimulsoft

I'm a very beginner of stimulsoft. I'm creating a report from my DB SQL but I've got problems with the lenght of the databand.
Basically, I would like to reduce the lenght of the databand because the data-source is different.
In the picture, I would like to trim the databand before the title "Descrizione". Can anyone help me?
Thank you so much
The bands have the heights only and it fills the whole width of its contained (here it is the page).
You need to use the Dock Style property that allows changing the position of the text components dynamically.
But you should check the order of the text components in the Report Tree.

Wrap text of Table cell in exported Excel in BIRT

I am using BIRT 4.4.1 in eclipse for report generation.
I would like to wrap my text of Table Header, it works fine in webviewer but when i exported to excel the table cell text is in a single line, and also the text is overflowed. I dont want to make the cell width equal to the content, I would like to wrap the text based on its container width.
I have set the cell width and label whitespace property as auto. is it correct, or what do i do to make it work in exported excel report?
In Webview
After exported, Excel view
In 4.4.2 export as XLS (not xlsx). As a workaround you can also add a line return within the text and that will appear correctly when you export to xlsx.
A third option that works with xlsx output is to select the text box in the cell, and in General Properties set the Whitespace to Preformatted. Then, either set the header cell size to a height that covers the required text OR in general properties of the entire report, set the layout to Auto Layout.

Number display as # in generated Excel file

I have an Excel file generated by JasperReports. What you can see here is the Excel's column will be display ### if the number is too long. I don't want to resize the width of column, i just want to display the number without seen as ### and stretch the height. I have tried set the column to Stretch with overflow and Stretch type = Relative to band height
This is the screenshot:
You will need to set the appropriate Jasper Reports excel property on your report element, or on the whole report iself. Documentation: http://jasperreports.sourceforge.net/sample.reference/xlsfeatures/
Specifically
net.sf.jasperreports.export.xls.column.width.ratio
If these properties are not available in your JasperReports version, you may have to simply fiddle around with the sizing to get something decent.

Watermark an image with reportviewer (full page report)

I know i can watermark an image with reportviewer 2012 in the body part of the page.
My report has header and footer, i need watermark an image in angle for the full page, from the bottom-left to the top-right and that will include the header, body and footer.
Any Idea? Thank you.
SSRS doesn't natively have a Watermark function, but you have a couple workarounds.
Fix 1 - Background Image
Create an image with the watermark you want (save as .bmp)
Add the image to the report
Set the Report Body Background property to use the image.
Body Properties
For Step 1, I like the following method:
Create a Word Document
Go to Page Layout > Watermark > Custom > and add whatever watermark you want
Save the Word Document as PDF.
Open the PDF and Select "Copy File to Clipboard"
This will create the correct sized image at 612x792. Paste into MSPaint and save as a bitmap. Here's a sample generated image that just says "TEST" that you can use.
Conditionally Applying:
Most times you have a watermark, you want to eventually remove it. Let's say you have a test and production report and you want to toggle the watermark. You can do it like this:
Add a Boolean Parameter, here called IsTest. It should look like this:
On the Body Properties menu, set the background image to use the following formula:
=iif(Parameters!IsTest.Value,"TestWatermark","")
When you're calling the report, make sure to pass in the parameter like this:
report.SetParameters(New ReportParameter("IsTest", appSettings.IsTestEnvironment))
Fix 2 - Text Field
Alternatively, you can do this with a text field for a little less heavy handed of an approach, but it cannot be rotated at a 45o angle.
Add a text field to the report. Make it large and grey.
Right click on the text field and click Send to Back so it appears behind all the other elements.
Make sure that any other fields with a white background have a transparent background, otherwise you'll get a blotchy display.
Further Reading
Watermark printing in SSRS report
How To Watermark your Report With Custom Image
Try if this will help. It shows several workarounds: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/b9c25fc1-00a5-4957-8f02-f91bf5984d76/how-to-achieve-watermark-underlay-feature-in-ssrs-2008

Let the paper size fit the print area in Excel

I wrote a 10*5 table in MS Excel 2010. Then I intended to print this table to a PDF file.
By default, it would print this table on a regular page, say Letter(8.5*11). This gives a printed paper with most area are blank.
Can I print this table to a PDF file with minimal blank space?
In another word, let the paper size fit the print area?
If that option did exist, the text would be very large since you effectively just zoomed in..
I recommend looking at your table in Page Layout view (goto View->Page Layout) and fiddling with the table's rows and columns until it takes up the entire space.
Yes you can by setting the normal print area to the area ytou want in the pdf and set the print margin settings to minimal and the Page setting to Fit to x by y pages.
If that would still leave you with emptiness on your pdf page your area is just to tiny. Increase its size by increasing the Text size inside that area until you see in the PDF save that you fill up the complete area.
Unfortunately there doesnt seem to be a single magic setting for this. You will have to find the size of the "paper" and that of your text yourself...

Resources