0

How to save the extracted variable value(text/content) in a pdf file using JSR223 PostProcessor in JMeter?? let say variable name ${data} . Please suggest some solutions.

Pratik Dey
  • 11
  • 3

1 Answers1

0

You will need a library like PDFBox for this

  1. Add it and all its dependencies to JMeter Classpath

  2. Restart JMeter to pick the .jars up

  3. The simplest code to write a JMeter Variable into a PDF file would be something like:

    def document = new org.apache.pdfbox.pdmodel.PDDocument()
    def page = new org.apache.pdfbox.pdmodel.PDPage()
    document.addPage(page)
    
    def font = org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA_BOLD
    
    // Start a new content stream which will "hold" the to be created content
    def contentStream = new org.apache.pdfbox.pdmodel.PDPageContentStream(document, page)
    
    // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
    contentStream.beginText()
    contentStream.setFont(font, 12)
    contentStream.moveTextPositionByAmount(100, 700)
    contentStream.drawString(vars.get('data'))
    contentStream.endText()
    
    // Make sure that the content stream is closed:
    contentStream.close()
    
    // Save the results and ensure that the document is properly closed:
    document.save('path-to-your-file.pdf')
    document.close()
    

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Hi @Dmitri, Thanks for your valuable comment. but when I have run the above snippet code get some error like " java.lang.IllegalArgumentException: U+000D ('controlCR') is not available in the font Helvetica-Bold, encoding: WinAnsiEncoding " , so can you please tell me what is this error ?? – Pratik Dey Jul 13 '22 at 10:12
  • Your `${data}` variable has some characters (i.e. line breaks) which are not allowed in PDF strings, you either need to remove the line breaks or split the `${data}` variable into an array of strings and call `drawSting()` function for each line: https://stackoverflow.com/questions/46644570/pdfbox-u000a-controllf-is-not-available-in-this-font-helvetica-encoding – Dmitri T Jul 13 '22 at 10:23