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

Convert tline into pdf file document in Java

former_member397166
Participant
0 Likes
1,921

Dear ALL,

         

     I built a RFC FM to convert smartform into tline which including PDF data.

the Code:

   Converting to PDF Format
  DATA l_lines TYPE TABLE OF tline WITH HEADER LINE.
*  DATA l_lines1 TYPE TABLE OF solix_tab WITH HEADER LINE.
  DATA l_docs TYPE TABLE OF docs.
  DATA len TYPE i.
  CALL FUNCTION 'CONVERT_OTF_2_PDF'
    IMPORTING
      bin_filesize           = len
    TABLES
      otf                    = job_output_info-otfdata[]
      doctab_archive         = l_docs[]
      lines                  = l_lines[]                          "this inner table include pdf file data
    EXCEPTIONS
      err_conv_not_possible? = 1
      err_otf_mc_noendmarker = 2
      OTHERS                 = 3.

  *CALL METHOD cl_gui_frontend_services=>gui_download
  *  EXPORTING
  *   bin_filesize = len
  *    filename     = 'c:\pdf.pdf'
  *    filetype     = 'BIN'
  *   CHANGING
  *    data_tab     = l_lines[]"i_objbin[]
  *  EXCEPTIONS
  *    OTHERS       = 1.

"If i want to save it in my PC ,call this FM CALL METHOD cl_gui_frontend_services=>gui_download.

But i dont want to do it.I want to pass the parameter l_lines[] to java(web) by RFC, and convert to pdf file and show in java(web).

thanks

Freddy


1 ACCEPTED SOLUTION
Read only

0 Likes
1,450

Hi Freddy,

I am not sure why you use CONVERT_OTF_2_PDF which returns the PDF indeed in the CHAR-like table LINES.

Can you use CONVERT_OTF instead? CONVERT_OTF allows to return the PDF data in an XSTRING field BIN_FILE.

Best regards,

Alexander

6 REPLIES 6
Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
1,450

Hi Freddy

In the RFC FM define a table parametr for these PDF lines and it will be returned when called via JAVA. then Java can use these lines to do display. What is the exact issue?

Nabheet

Read only

0 Likes
1,450

nabheet

     How to use the lines to do display in Java?

     Is there a way to convert the PDF lines to XString in RFC FM and convert XString to PDF file in Java?

thanks

Freddy

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,450

Hi,

I have not tested this.

But if I am not mistaken the 'CONVERT_OTF_2_PDF' output is PDF file hidden in a table.

If this is the case just set LINES as your output from the RFC function.

if you use servlet maybe you can even convert the table into something that can be sent using the

HttpServletResponse .

Regards.

Read only

0 Likes
1,451

Hi Freddy,

I am not sure why you use CONVERT_OTF_2_PDF which returns the PDF indeed in the CHAR-like table LINES.

Can you use CONVERT_OTF instead? CONVERT_OTF allows to return the PDF data in an XSTRING field BIN_FILE.

Best regards,

Alexander

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,450

Hi,

Update:

I have tested this and here is some code:

SAP:(I got the OTF from CALL FUNCTION 'PRINT_TEXT')

*----------------------------------------------------------------------*
FORM get_pdf_data
  USING
    tdname TYPE thead-tdname
  CHANGING
    it_tline TYPE text_line_tab
    bin_file TYPE xstring .

  DATA: st_thead TYPE thead .

  DATA: it_lines TYPE tline_tab .

  st_thead-tdobject = 'TEXT' .
  st_thead-tdname   = tdname .
  st_thead-tdid     = 'ST' .
  st_thead-tdspras  = 'E'  .

  CALL FUNCTION 'READ_TEXT'
    EXPORTING
      id                      = st_thead-tdid
      language                = st_thead-tdspras
      name                    = st_thead-tdname
      object                  = st_thead-tdobject
    IMPORTING
      header                  = st_thead
    TABLES
      lines                   = it_lines
    EXCEPTIONS
      id                      = 1
      language                = 2
      name                    = 3
      not_found               = 4
      object                  = 5
      reference_check         = 6
      wrong_access_to_archive = 7
      OTHERS                  = 8.

  IF sy-subrc NE 0 .
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .
  ENDIF.

  DATA: it_otfdata TYPE otf_t_itcoo .

  DATA: st_options TYPE itcpo.

  st_options-tdgetotf = abap_true .

  CALL FUNCTION 'PRINT_TEXT'
    EXPORTING
      dialog                   = abap_false
      header                   = st_thead
      OPTIONS                  = st_options
    TABLES
      lines                    = it_lines
      otfdata                  = it_otfdata
    EXCEPTIONS
      canceled                 = 1
      device                   = 2
      form                     = 3
      OPTIONS                  = 4
      unclosed                 = 5
      unknown                  = 6
      format                   = 7
      textformat               = 8
      communication            = 9
      bad_pageformat_for_print = 10
      OTHERS                   = 11.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  DATA: it_otfdata_m TYPE otf_t_itcoo .

* "Merge pdf output demo"
  APPEND LINES OF it_otfdata TO it_otfdata_m .

* DATA: bin_file TYPE xstring .
  DATA: it_lines_dummy TYPE tline_tab .
  DATA: bin_filesize TYPE i .

  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
    IMPORTING
      bin_file              = bin_file
      bin_filesize          = bin_filesize
    TABLES
      otf                   = it_otfdata_m
      lines                 = it_lines_dummy
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4
      OTHERS                = 5.

ENDFORM.                    " GET_PDF_DATA


Java:

final byte[] byteArray = function.getExportParameterList().getByteArray("BIN_FILE");

final Path path = Paths.get("My.pdf");

Files.write(path, byteArray, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);

regards.

Another update:

Using in HttpServlet:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.ext.Environment;
import destinations.MyDestination;

public class MyServlet extends HttpServlet {

@Override
protected void doGet(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {

  httpServletResponse.setContentType("application/pdf");
  httpServletResponse.setStatus(HttpServletResponse.SC_OK);

  final ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();

  servletOutputStream.write(getPDF());

  servletOutputStream.flush();
  servletOutputStream.close();

}

private byte[] getPDF() {

  try {

   Environment.registerDestinationDataProvider(new MyDestination());
   final JCoDestination destination = JCoDestinationManager.getDestination(destinations.Id.sapdev2.name());
   final JCoRepository repository = destination.getRepository();

   final JCoFunction function = repository.getFunction("Y_R_EITAN_TESTS_03");

   function.execute(destination);

   final byte[] byteArray = function.getExportParameterList().getByteArray("BIN_FILE");

   return byteArray;

  } catch (final JCoException exception) {
   exception.printStackTrace();
   return null;
  }

}

}

Read only

former_member397166
Participant
0 Likes
1,450

Thanks ALL, Accomplish.