Announcement

Collapse

Mirth Connect 4.0.1 Released!

Mirth Connect 4.0.1 is now available as an appliance update and on our GitHub page. Mirth Connect 4.0.1 is a patch release containing a bug fix which includes fixing a Jetty keystore regression that caused Connect servers using a PKCS12 keystore containing a wildcard certificate and/or a certificate with a SAN to throw an exception on startup. See the release notes for the list of fixes and updates.

Download | See What's New | Upgrade Guide | Release Notes

For discussion on this release, see this thread.
See more
See less

Transform base 64 image to base64 pdf

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transform base 64 image to base64 pdf

    Hello,

    i would like to transform a base64 image in base64 pdf.
    I received from a CDA file an image encoded base64 and i would like to transform this image in a pdf encoded base 64.

    I thought of a possibility which consists in creating 3 connectors:
    • A first connector retrieves the image encoded in base 64, with a channel writer as its destination.
    • A second connector transforms the image base64 received from the first connector into a PDF (file writer in binary mode) and deposits the PDF locally.
    • A third connector retrieves the PDF deposited locally and transforms it into base64, in order to reinject it into the first connector.
    I find it complex.
    Wouldn't there be an easier solution allowing me to transform the base64 encoded image into a base64 encoded pdf.

    Thank you for you precious help.

  • #2
    This can be done in a JavaScript transformer step.

    You have to
    1. Decode your base64 image
    2. Transform it to pdf e.g. via iText
    3. Encode the pdf into base64
    imageToPdfA.png

    Unfortunately the forum again does not let me copy/paste code thus I had to use a screenshot instead.

    Download and import this code template: imageToPdf()_v1.1.xml
    Last edited by odo; 05-04-2022, 06:32 AM. Reason: Added optional support for generation of PDF/A documents

    Comment


    • #3
      Is the PDF in the CDA the image only? Or is it a PDF with text and an image? A sample CDA here would be helpful.
      Diridium Technologies, Inc.
      https://diridium.com

      Comment


      • #4
        Originally posted by odo View Post
        This can be done in a JavaScript transformer step.

        You have to
        1. Decode your base64 image
        2. Transform it to pdf e.g. via iText
        3. Encode the pdf into base64
        The code could look like follows (I did not test it, so it might contain bugs):


        Unfortunately the forum again does not let me copy/paste code thus I had to use a screenshot instead. This example uses iText 5.
        Thank you for your reply and your help.
        Attached is the channel with your code but it does not work.

        I have an error :
        DETAILS: ReferenceError: "PageSize" is not defined

        Attached Files

        Comment


        • #5
          Originally posted by pacmano View Post
          Is the PDF in the CDA the image only? Or is it a PDF with text and an image? A sample CDA here would be helpful.
          Thank you pacmano for your reply and help.

          Yes image and PDF are similar.

          I just want to transform IMAGE BASE 64 into PDF BASE64, without adding text.


          The CDA is a test, i just extract base64 of this CDA in a variable, so it does not represent a real interest, but i attach a photo of this CDA.
          If you need more, I am at your disposal
          Attached Files

          Comment


          • #6
            I do not understand "Yes image and PDF are similar."

            Do you mean "The PDF is the image, there is nothing else in the PDF other than the image"?

            If yes, you can just extract and save the base64 version, you don't need to parse its contents at all.

            OR is the base64 in the XML an image, not a PDF?
            Diridium Technologies, Inc.
            https://diridium.com

            Comment


            • #7
              Originally posted by pacmano View Post
              I do not understand "Yes image and PDF are similar."

              Do you mean "The PDF is the image, there is nothing else in the PDF other than the image"?

              If yes, you can just extract and save the base64 version, you don't need to parse its contents at all.

              OR is the base64 in the XML an image, not a PDF?

              Oh, sorry for expressing myself badly. I meant that in the XML I have an image (encoded in base 64), not a PDF.

              I would like to transform my XML so that instead of the image (encoded in base64), I have a pdf (encoded in base64) corresponding to my starting image

              Comment


              • #8
                Another way:

                NOTE THE STUPID FORUM SOFTWARE ADDS EXTRA SPACES SOMETIMES

                add this the mcservice.vmoptions:

                -Djava.protocol.handler.pkgs=org.xhtmlrenderer.prot ocols
                -classpath/a extensions/doc/lib/flying-saucer-core-9.0.1.jar


                Let's assume you collect the base64 image into a channelMap variable named "image",

                In a document writer you can then create a PDF with:

                2022-04-14_11-57.jpg

                In my source XML:


                <xml><image> the base64 string is here </image></xml>

                in the attachment viewer:


                2022-04-14_11-59.jpg


                Diridium Technologies, Inc.
                https://diridium.com

                Comment


                • #9
                  I would use the attachment handler to save space in general here, this is a great article: https://forums.mirthproject.io/artic...ng-attachments

                  In your case you can grab the image in the XML as an attachment.
                  Diridium Technologies, Inc.
                  https://diridium.com

                  Comment


                  • #10
                    Originally posted by vincentsm33 View Post
                    I have an error :
                    DETAILS: ReferenceError: "PageSize" is not defined
                    Use com.itextpdf.text.PageSize instead of PageSize

                    Comment


                    • #11
                      Originally posted by odo View Post

                      Use com.itextpdf.text.PageSize instead of PageSize
                      I already try this by searching on Google but i have an error :

                      "cannot read property "A4" of null"

                      Thank you !

                      Comment


                      • #12
                        Please find a tested and fixed version in my initial post.

                        This version is using iText 5. Please also assure that iText 5 is supported by your mirth version.

                        Comment


                        • #13
                          Originally posted by odo View Post
                          Please find a tested and fixed version in my initial post.

                          This version is using iText 5. Please also assure that iText 5 is supported by your mirth version.
                          Slim ! I found the cause of the error. Mirth now uses PDFBOX
                          Attached Files

                          Comment


                          • #14
                            Originally posted by pacmano View Post
                            I would use the attachment handler to save space in general here, this is a great article: https://forums.mirthproject.io/artic...ng-attachments

                            In your case you can grab the image in the XML as an attachment.
                            Pacmano, I followed your recommendations, and in my output connector I have my attachment in PDF form.

                            On the other hand, I do not understand how to transcribe it into pdf in base64 format in a variable in order to put it in my starting XML file.

                            Comment


                            • #15
                              Either in your response transformer for that step or the next destination, build a new XML message. The outbound message template would be the stub of your new XML message. This forum has posts on how to get an attachment once created.
                              Diridium Technologies, Inc.
                              https://diridium.com

                              Comment

                              Working...
                              X