PDF export images
PDF export images
dvpriyanka
Posts: 5Questions: 1Answers: 0
Hi to all,
I exported the table data into pdf.I have an image column in the table.
After exporting this column appears as blank column instead of holding images.
How i can encode my images to base64 format?
This is a repeated question.I am unable to get pdfmake API site.
Thanks
Subi
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi,
I encoded my image and put in src of image tag as below.Then also image column in pdf appears blank.Is this correct?
<td><img src=<%="\'data:rotary;base64,"+encodedImage+"\'," %> border="1" alt="img not available" height="50" width="50"></td>
"encodedImage " is the variable that holds the encoded form of image.
Thanks
Subi
You need to use the base64 string inside the pdfmake document, not your HTML. See the pdfmake examples for images (click the "Images" link need the top) to see how you can use images in that library.
Use the
customize
callback of thepdfHtml5
button type to be able to modify the pdfmake document structure that Buttons creates.Allan
Thank you Allan.I am going to work on this.
Hi Allan,
I have gone through the link you specified.But I couldn't find an apt solution.
I am getting table data from the database and images from file.After executing the code I couldnt see the images in my datatable as well as in my exported page(pdf,excel...).The image column is the 2nd column in the table and appears as blank.
I found a discussion in the below link, but the referenced url in this page was not found.
https://datatables.net/forums/discussion/comment/99532/#Comment_99532.
How to give "for loop" for getting the images.Also i am using a java utility for converting the image to base64 format.
I can give you the pseudocode of what i want.
This is my code...
//Is this the correct place to add column content of table?
//I want to add only the images in 2nd column.Remaining data i have included in <td></td> of table in html code.But in html code also, i have this image column.If i code here, will i get images in datatable?
The Excel and PDF image insertion will be completely different implementations I'm afraid. I've never attempted to insert an image into an Excel generated document, so you would need to read the open spreadsheet specification to see how that might be done - I suspect you'll need to put the images into the generated zip structure and then reference them using some specific XML tag in the spreadsheet.
For the PDF, you'll need to loop over the rows in the pdfmake document structure that you create and insert the base64 image into the required cell.
Neither a trivial task I'm afraid, which is why it isn't provided out of the box.
Allan
Hi Allan,
For the PDF, you'll need to loop over the rows in the pdfmake document structure that you create and insert the base64 image into the required cell.
The above answer is not clear to me.Can u give me some sample code?
If you provide some dummy codes it will be more help for me.
Sorry, I don't have any example code of what you are looking for. As I say, it is not a trivial task.
Allan
Hey I'm stuck to the same thing..
did you get the answer how to put image data in pdf?
Hi everyone, i found a solution and i want to share with all.
Asp.net MVC 5 (Razor)
Important things:
* Pdfmake docs: http://pdfmake.org/playground.html
* exportOptions: false (This is required to include img html data in final document. This html will be changed in the button callback)
Steps:
public static string GetImageBase64(string PhotoPath)
{
if (!string.IsNullOrEmpty(PhotoPath) && File.Exists(PhotoPath))
{
Image img = Image.FromFile(PhotoPath);
<td align="center"><img src="@item.Base64Image" width="80" alt="@item.FullName" style="border-radius:5px;" /></td>
buttons: [
{
extend: 'pdfHtml5',
title: 'Document Tittle',
customize: function (doc) {
]
Thats it. enjoy it