0

I'm trying to print (the action of sending to printer) a PNG image I get as a response from my ajax call. The response comes as an "inline" image/png type response from my View. The reply from ajax is successful, meaning the image is properly sent. I can even open it in another window. However, I want to send that image automatically to print. The window itself has other info I don't want to get mixed with the response, thus I need to print only the image I'm getting. I can do window.print() but that just prints the current page, not the response (obviously). Does anyone know how to process the response in order to print it?

function traer_1234(){
        $.ajax({
            dataType: "html",
            contentType: "application/json",
            type: 'GET',
            data: {"nanda": 1},
            url: "{% url 'View_test' %}",
            success: function (response) {
                alert("1234")
                window.print()
            },
            error: function (response) { 
                alert("Error") 
            }
        })
    }

The View side looks like this

def View_test(request):

    ean=barcode.codex.Code39("int-12345",writer=ImageWriter(), add_checksum= False)
    image = ean.render()

    response = HttpResponse( headers={
        'Content-Type': 'image/png',
        'Content-Disposition': 'inline; filename="borrar.png"',
    })

    image.save(response, "PNG")

    return response

Thanks in advance to anyone who can supply any info.

Jas
  • 33
  • 4
  • Provide your ajax response how you're getting your image – Ankit Tiwari Aug 13 '22 at 18:11
  • Does this answer your question? [Print
    only?](https://stackoverflow.com/questions/468881/print-div-id-printarea-div-only)
    – Ankit Tiwari Aug 13 '22 at 18:12
  • The response I get back is an inline image as png. I want to get that automatically sent to the printer, not place anywhere in my html. – Jas Aug 14 '22 at 01:04
  • Hello @Jas can you provide your response inside your question by making an [edit] – Ankit Tiwari Aug 14 '22 at 07:10
  • Hello @AnkitTiwari, I'm not sure what specifically you mean by the "response", could you please clarify? The response itself is a PNG image, at the moment is sent from the View module as "inline" thus I can straight forward open it in another window, however if I would send it as an "attachment" I can even download it. – Jas Aug 15 '22 at 02:05
  • Hello @Jas means response your image is a url or a binary data ? – Ankit Tiwari Aug 15 '22 at 13:50
  • hi @AnkitTiwari it is an image, I just added the Vieew part of the code to the initial post. – Jas Aug 15 '22 at 19:58
  • Does this answer to your question? [How to check file arrived in JavaScript and HttpResponse object in Django](https://stackoverflow.com/a/70533526/14457833) – Ankit Tiwari Aug 16 '22 at 06:06
  • No, as mentioned throughout the post and comments, I want to send to print the ajax response (the response is successful, I can wither download it or open it in another window if set to it in the View part). What I need is to print through jquery the ajax response directly. – Jas Aug 16 '22 at 23:28

0 Answers0