How to save a string as a PDF

Hello,

I would like to save a string as a PDF. I tried the following method, but it did not produce a valid file.

my_string = "this is my string"
file = open("test.pdf", "w")
write(file, my_string)
close(file)

Is there a way to save my_string as a PDF?

Thanks!

I found a way, but it is ugly. render from Latexify will create a PDF from a LaTeXString in a temporary directory. I then just copied those temporary pdfs somewhere accessible.

using LaTeXStrings, Latexify, Glob
my_string = "this is my string"
my_string |> LaTeXString |> render
source = glob("*.pdf", tempdir())
dest = "./figures/"  # wherever you want to save the pdfs
for file in source
    cp(file, joinpath(dest, basename(file)))
end
2 Likes

Thank you. I will keep this thread open in case there is a simpler approach, but this looks feasible.

If you’re fine with using an external tool like LaTex, there is also a Pandoc wrapper

2 Likes

Another idea:

using Luxor
my_string = "this is my string"
@pdf text(my_string, halign=:center) 400 200 "filename.pdf"

Adjust font, color, and dimensions to taste…

6 Likes

Thanks for your recommendation. I like the simplicity of the @pdf macro, but one problem I noticed is that it does not seem to work with formatting such as \n. Instead, the text is placed on one line which runs off the page (see below). Are there any workarounds?

str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempor quam vel sem mollis, sed lobortis lectus laoreet. Morbi iaculis, turpis vitae congue tincidunt, dolor diam consequat neque, in aliquam massa ex ac lacus. Vivamus congue vitae erat in hendrerit. Suspendisse eget dolor vitae diam vehicula cursus.  \n Suspendisse at congue nisi. Ut quis nisl erat. Nam in nisl ut massa varius lobortis. Nunc id porttitor enim, nec vestibulum eros. Suspendisse congue consectetur neque, sodales finibus ante accumsan non. Donec et blandit magna. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed vulputate est eu leo venenatis, ac tincidunt mauris scelerisque. Donec eget justo eget mauris consequat euismod. Morbi nulla purus, pulvinar non tincidunt commodo, sodales ut ante.
Fusce ut tempus lacus, sit amet fringilla magna. Cras non fringilla magna, eu mollis tortor. Vivamus volutpat nibh metus. Suspendisse sed erat eros. Suspendisse eget ullamcorper lacus. Aenean pulvinar, lorem non maximus gravida, turpis erat laoreet lectus, at scelerisque lacus ipsum sit amet tortor.\n  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla id accumsan libero.
Curabitur mollis arcu enim, vitae volutpat risus sagittis nec. Curabitur tempor justo eget arcu molestie fermentum. Maecenas et ultrices augue. Sed mi est, iaculis vel massa ut, consequat sagittis felis. Maecenas cursus odio arcu, eu tristique urna tempor vulputate. Proin imperdiet fringilla eros vel egestas. Vestibulum interdum at nulla quis tempor."

Well, it’s not intended to be a replacement for InDesign or Illustrator :slight_smile: , but the manual will reveal a few tools that you might be able to use, such as textwrap():

5 Likes

Using Plots:

using Plots; gr(dpi=1200)

str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempor quam vel sem mollis, sed lobortis lectus laoreet. Morbi iaculis, turpis vitae congue tincidunt, dolor diam consequat neque, in aliquam massa ex ac lacus. Vivamus congue vitae erat in hendrerit. Suspendisse eget dolor vitae diam vehicula cursus.  \n Suspendisse at congue nisi. Ut quis nisl erat. Nam in nisl ut massa varius lobortis. Nunc id porttitor enim, nec vestibulum eros. Suspendisse congue consectetur neque, sodales finibus ante accumsan non. Donec et blandit magna. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed vulputate est eu leo venenatis, ac tincidunt mauris scelerisque. Donec eget justo eget mauris consequat euismod. Morbi nulla purus, pulvinar non tincidunt commodo, sodales ut ante.
Fusce ut tempus lacus, sit amet fringilla magna. Cras non fringilla magna, eu mollis tortor. Vivamus volutpat nibh metus. Suspendisse sed erat eros. Suspendisse eget ullamcorper lacus. Aenean pulvinar, lorem non maximus gravida, turpis erat laoreet lectus, at scelerisque lacus ipsum sit amet tortor.\n  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla id accumsan libero.
Curabitur mollis arcu enim, vitae volutpat risus sagittis nec. Curabitur tempor justo eget arcu molestie fermentum. Maecenas et ultrices augue. Sed mi est, iaculis vel massa ut, consequat sagittis felis. Maecenas cursus odio arcu, eu tristique urna tempor vulputate. Proin imperdiet fringilla eros vel egestas. Vestibulum interdum at nulla quis tempor."

plot(framestyle=:none, size=(2000, 200))
annotate!(0, 0.5, text(str, 5, :left, :blue, "Computer Modern"))

savefig("plot_text_to_pdf_1200dpi.pdf")
3 Likes

Thanks. This gets me close to what I need. Sorry I forgot one detail: the text spans an unknown number of pages. Do you know whether Luxor can handle that? (Plots did not seem to handle automatic page breaks either).

using Luxor 
str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempor quam vel sem mollis, sed lobortis lectus laoreet. Morbi iaculis, turpis vitae congue tincidunt, dolor diam consequat neque, in aliquam massa ex ac lacus. Vivamus congue vitae erat in hendrerit. Suspendisse eget dolor vitae diam vehicula cursus.  \n Suspendisse at congue nisi. Ut quis nisl erat. Nam in nisl ut massa varius lobortis. Nunc id porttitor enim, nec vestibulum eros. Suspendisse congue consectetur neque, sodales finibus ante accumsan non. Donec et blandit magna. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed vulputate est eu leo venenatis, ac tincidunt mauris scelerisque. Donec eget justo eget mauris consequat euismod. Morbi nulla purus, pulvinar non tincidunt commodo, sodales ut ante.
Fusce ut tempus lacus, sit amet fringilla magna. Cras non fringilla magna, eu mollis tortor. Vivamus volutpat nibh metus. Suspendisse sed erat eros. Suspendisse eget ullamcorper lacus. Aenean pulvinar, lorem non maximus gravida, turpis erat laoreet lectus, at scelerisque lacus ipsum sit amet tortor.\n  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla id accumsan libero.
Curabitur mollis arcu enim, vitae volutpat risus sagittis nec. Curabitur tempor justo eget arcu molestie fermentum. Maecenas et ultrices augue. Sed mi est, iaculis vel massa ut, consequat sagittis felis. Maecenas cursus odio arcu, eu tristique urna tempor vulputate. Proin imperdiet fringilla eros vel egestas. Vestibulum interdum at nulla quis tempor."

for _ ∈ 1:10 
    str *= str
end

@pdf begin 
    fontsize(14)
    textwrap(str, 500, Point(-300, -100))
end 800 500 "file_name.pdf"

If there is not a way to cleanly automate page breaks with any of the approaches above, maybe I can save as docx (which has page breaks and formatting) and then convert to pdf.

It really sounds like you would be better of with a more general text-formatting tool, for example the Pandoc.jl package mentioned above.

4 Likes

In fact what Christopher calls a string, is actually an entire book :slight_smile:

5 Likes

page breaks…

Yes you’re now moving into document-generation territory.