Skip to Content
-1

Converting HTML to PDF in ABAP

Jun 23, 2017 at 03:01 PM

1.1k

avatar image
Former Member

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.

10 |10000 characters needed characters left characters exceeded

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

0
Former Member

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

0
* Please Login or Register to Answer, Follow or Comment.

5 Answers

Best Answer
Sandra Rossi Jun 24, 2017 at 06:38 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded
Richard Harper Jun 25, 2017 at 06:50 AM
1

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

Show 8 Share
10 |10000 characters needed characters left characters exceeded

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

0

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

0
Former Member

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.

0

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.

0
Former Member
Richard Harper

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.

0

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

0
Former Member
Richard Harper

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.

0

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 ?

0
Horst Keller
Jun 25, 2017 at 01:22 PM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
Mehmet Dagnilak Nov 22, 2017 at 11:22 AM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
Kirill Gorin Jun 26, 2017 at 08:40 AM
0

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

That's basically what I am suggesting.

0