Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Converting HTML to PDF in ABAP

purayil_t
Explorer
22,803

Hello,

I have requirement of sending pdf document as an attachment in email. But the scenario is not straight forward. I am getting my data in JSON format from the client, I then convert the JSON to XML and apply XSL transformation to convert to HTML.

I now want to convert the HTML to PDF. Is there any way in ABAP to convert the HTML content to PDF content.

Ex: I get my HTML content something like this in ABAP.

data : lv_html type string.

lv_html = '<table> <tr> <td>Column 1</td> <td>Column 2</td> </tr> </table>'.

Regards,

Purayil.

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
8,141

ABAP systems are not universal converters, they only convert SAP proprietary formats (sapscript, smart forms, ABAP lists). You must use a dedicated software.

18 REPLIES 18
Read only

Sandra_Rossi
Active Contributor
8,142

ABAP systems are not universal converters, they only convert SAP proprietary formats (sapscript, smart forms, ABAP lists). You must use a dedicated software.

Read only

Former Member
8,141

I disagree....

I would get my data into an internal table via a simple transformation. Then I would output a smartform which is a representation of the document I have to send to spool and convert the OTF to PDF - all proven technology.

Rich

Read only

0 Likes
8,141

I agree, SAP can convert proprietary format OTF (smart forms) to PDF. But not HTML to PDF.

Read only

0 Likes
8,141

Hi Richard,

I tried this by adding it to internal table and converting to PDF via FM CONVERT_OTF. As Sandra said, it needs to be in an OTF format. The function module gave an error BAD_OTF because the format is incorrect. It expects the contents to be in a particular format in the table OTF with TDPRINTCOM and TDPRINTPAR.

@Sandra, thanks for your reply. I should be looking to do it in Java and call this from ABAP.

Regards,

Purayil.

Read only

0 Likes
8,141

True, however, I was proposing an alternative solution. So my disagreement was not in the fact that you can't convert HTML but in the implication that you can't do it full stop....

Rich

Read only

0 Likes
8,141

You're missing the smartform output:

"Then I would output a smartform which is a representation of the document"

That is where the OTF conversion comes in.

"I should be looking to do it in Java and call this from ABAP."

Nope - that just really complicates things and is not needed.

Read only

0 Likes
8,141

Hello Richard,

Thanks for your response, My HTML is dynamic, ABAP does not have any idea of the HTML content.

I don't have much knowledge on smartfomrs, if I am correct the smartfom should have a template which if kind of fixed.

Correct me if I am wrong.

Regards,

Purayil.

Read only

0 Likes
8,141

Hi.

Is your html a fixed message with variable parts ? If so then a smartforms can handle that easily. If it is totally dynamic then parse the html, output it to a standard text and use that text in the smartform. Smartforms are very dynamic.

Rich

Read only

0 Likes
8,141

Hello Richard,

My HTML is completely dynamic, I have tables, radio buttons, list items etc. and also with CSS. How do you mean to parse it to standard text?

Purayil.

Read only

0 Likes
8,141

You can create and read standard texts on the fly using function modules SAVE_TEXT and READ_TEXT. You say it is completely dynamic - Surely it must conform to some sort of structure ?

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
8,141

And something more far fetched - don´t know if it is feasible:

Send HTML via OLE automation to Word and let the desktop application do the conversion.

Read only

Former Member
0 Likes
8,141

If you have access to GUI you can load html into CL_GUI_HTML_VIEWER then send it to spool and try to convert spool to pdf with CONVERT_ABAPSPOOLJOB_2_PDF.

Read only

0 Likes
8,141

That's basically what I am suggesting.

Read only

matt
Active Contributor
0 Likes
8,141

I've converted some of the comments to answers. Just so people know what has happened.

Read only

tauqiram
Newcomer
0 Likes
8,141

Hi Purayil,

Did you solve your issue? Can you please share your solution? Many thanks.

I tried sending HTML to smartform and generated smartform output from spool into PDF but unfortunately it is in HTML. Smartform print it as html string with tags. I think we need some kind of container to render html and get output from there, but in WebUI we can not use CL_GUI_HTML_VIEWER. Any other idea?

BR,

Tauqir

Read only

dagni
Participant
8,141

I had this requirement too some time ago. In my case, I had to combine several html files into one PDF file. To achieve this, I used a 3rd party command line tool named wkhtmltopdf (https://wkhtmltopdf.org , https://github.com/wkhtmltopdf/wkhtmltopdf). To use it, you save the html files to a folder, then run this command line tool with the names of the files and a .pdf file name. Then you read the generated pdf file.

Since it doesn't need to display anything, it can run in the background. I have created a web service which has html file contents as input (array of string) and pdf content as output (xstring). It runs on Tomcat on a separate server. I call that web service from Abap.

Read only

Former Member
0 Likes
8,141

Ran into the same once yet I decided to not to resolve it manually and used the tools found through the github forum and some googling. There are dozens of free tool to convert html to pdf but, as you may suggest, all of them are doing so like a crap and there's no chance you could get away with not to edit it manually. I did so with this editing tool ds11.pdffiller.com because it's the cheapest one I've found back then. Short of features yet it worth it

Read only

Ametrin
Newcomer
0 Likes
5,296

Hi Purayil,

ABAP doesn’t offer a built-in way to convert arbitrary HTML to PDF—it mainly supports conversion for SAP-specific formats like Smart Forms, SAPscript, or ALV lists. For your case, where you're working with dynamic HTML, you'll need to use an external solution.

One option is to use a dedicated HTML-to-PDF conversion API, such as PDFBolt. With such an API, you can send your HTML content via an HTTP request and receive a PDF in response, which you can then attach to your email using ABAP’s standard emailing functions (e.g., BCS).

Alternatively, if your environment allows, you might set up a command-line tool on your server that performs the conversion, and then call that tool from ABAP.

Hope this helps!