Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using MATA to create a PDF, how to set the Page Size (correctly)?

    Hi, in Stata 14, it is now possible to create a PDF using mata, something I appreciate very much.
    But, I am struggling to get the 'basics' to work. For example, with this code, from the documentation:
    Code:
    mata:
    pdf = PdfDocument()
    
    p = PdfParagraph()
    p.addString("This is our first example of a paragraph. ")
    p.addString("Let's add another sentence. ")
    p.addString("Now we will conclude our first paragraph.")
    pdf.addParagraph(p)
    
    pdf.save("WorkingExample1.pdf")
    pdf.close()
    * clean up
    mata drop p
    mata drop pdf
    end
    Mata does create a PDF in the current working directory. This is straightforward.
    So, now I want to set some document specifics, like the page size, which requires the PdfDocument class command setPageSize(sz)which I have coded as:
    Code:
    pdf.setPageSize(A4)
    Code:
    mata:
    pdf = PdfDocument()
    
    p = PdfParagraph()
    p.addString("This is our first example of a paragraph. ")
    p.addString("Let's add another sentence. ")
    p.addString("Now we will conclude our first paragraph.")
    pdf.addParagraph(p)
    
    pdf.setPageSize(A4)
    pdf.save("WorkingExample1.pdf")
    pdf.close()
    * clean up
    mata drop p
    mata drop pdf
    end
    But this results in an error:
    <istmt>: 3499 A4 not found
    (5 lines skipped)


    Does anyone know what goes wrong here, or if am not correctly coding?

    Best regards,
    Eric

    {Note that I posted this question earlier but in the wrong Forum}
    http://publicationslist.org/eric.melse

  • #2
    Note that the manual states: setPageSize(sz) sets the page size of the PDF file. The possible values for size are Letter, Legal, A3, A4, A5, B4, and B5.
    I tried:
    Code:
    setPageSize("A4")
    but that also results in an error:
    invalid expression
    (2 lines skipped
    http://publicationslist.org/eric.melse

    Comment


    • #3
      The "pdf." prefix is necessary to identify the object you created using PdfDocument to which setPageSize is to be applied.
      Code:
      pdf.setPageSize("A4")

      Comment


      • #4
        Dear William,

        Yes, I was incomplete. However, this code still results in an error
        Code:
        mata:
        pdf = PdfDocument()
        
        p = PdfParagraph()
        p.addString("This is our first example of a paragraph. ")
        p.addString("Let's add another sentence. ")
        p.addString("Now we will conclude our first paragraph.")
        pdf.addParagraph(p)
        
        pdf.setPageSize("A4")
        pdf.save("WorkingExample1.pdf")
        pdf.close()
        * clean up
        mata drop p
        mata drop pdf
        end
        invalid expression
        (2 lines skipped)


        http://publicationslist.org/eric.melse

        Comment


        • #5
          The error is caused by the line

          Code:
          * clean up
          , change the line to

          Code:
          // clean up
          should fix the problem. Since Mata uses "*a" for variable a of the pointer type, using "*" to start a comment line is not valid in Mata.

          Last edited by Hua Peng (StataCorp); 24 May 2015, 16:46.

          Comment


          • #6
            Dear Hua (and William), thank you for pointing this out to me! The code works just fine now.
            http://publicationslist.org/eric.melse

            Comment

            Working...
            X