Save/Export to PDF and internal links

Victor Warner's Avatar

Victor Warner

06 Oct, 2013 01:04 PM

Purchaser of Marked 1 and Marked 2 and very *non* technical user.

I would like to know if Marked 2 will support internal links on export/save to PDF. At the moment it does not appear to do so.

I can see that in this Forum there has been discussion about this and that it is possible using a utility such as wkhtlmltopdf to support internal linking (see attached file as to what I mean by an internal link).

But knowledge is too limited to work out by wkhtmltopdf can support internal linking but Marked 2 cannot.

Is this something that Marked 2 can do (or will be coming)?

  1. Support Staff 1 Posted by Brett on 06 Oct, 2013 02:38 PM

    Brett's Avatar

    As mentioned in other discussions, it's a webkit bug, which is what Marked uses to display the preview and export PDFs. The bug has been fixed in the open source version of WebKit, but hasn't been integrated by Apple. When they do, it will work, but until then, not within Marked.

    wkhtmltopdf uses it's own patched version of WebKit, which I may have to resort to at some point.

  2. 2 Posted by Victor Warner on 07 Oct, 2013 08:44 AM

    Victor Warner's Avatar

    Thank you for the explanation.

  3. 3 Posted by potsky on 23 Oct, 2013 09:45 PM

    potsky's Avatar

    Perhaps available on Maverick with Safari 7 ?

    Currently downloading the upgrade, I'll try tomorrow but it is a pity that readers can not jump on page 367 in the PDF !

  4. 4 Posted by Victor Warner on 24 Oct, 2013 07:02 AM

    Victor Warner's Avatar

    I tried with Mavericks and latest version of Marked (2.1/777): no difference.

  5. 5 Posted by potsky on 24 Oct, 2013 07:22 AM

    potsky's Avatar

    What a shame...

  6. 6 Posted by potsky on 24 Oct, 2013 09:07 PM

    potsky's Avatar

    Ok, it is not so hard to achieve I think...

    I have downloaded wkhtmltopdf for mac os x from the googlecode page.
    I have saved my Marked document in HTML instead of PDF.

    Then I execute this in a Terminal this command ./wkhtmltopdf marked.html output.pdf
    I have something like this :

    (potsky@rb-macbookpro:~)$ cd Desktop/
    (potsky@rb-macbookpro:~/Desktop)$ cd wkhtmltopdf.app/Contents/MacOS/
    (potsky@rb-macbookpro:~/Desktop/wkhtmltopdf.app/Contents/MacOS)$ ./wkhtmltopdf ~/Desktop/ma
    MacBookPro.png  marked.html
    (potsky@rb-macbookpro:~/Desktop/wkhtmltopdf.app/Contents/MacOS)$ ./wkhtmltopdf ~/Desktop/marked.html ~/Desktop/marked.pdf
    Loading pages (1/6)
    Counting pages (2/6)
    Resolving links (4/6)
    Loading headers and footers (5/6)
    Printing pages (6/6)
    Done
    (potsky@rb-macbookpro:~/Desktop/wkhtmltopdf.app/Contents/MacOS)$
    

    Then when I open my output.pdf, I have all images, links in the TOC are ok and better... I have a TOC on a drawer in Preview.app !!!

    3 problems :

    • the font of my css is not ok, perhaps I have to include it somewhere
    • the Marked rule Replace HR with page breaks does not work
    • keyword <!--BREAK--> does not work

    So :

    1. Could a Marked developer explain us how they convert the BREAK and how they manage font before feeding the safari converter ? Then I could code a small bash or python script to automate this process ?

    2. Why don't you include wkhtmltopdf in the Marked binary and use it instead the Mac OSX built in version ?

      • If it is a licence problem, perhaps could you include an option named Path to the pdf converter and use it instead ?

    :-) I am happy because there is a solution !

  7. Support Staff 7 Posted by Brett on 24 Oct, 2013 09:12 PM

    Brett's Avatar

    1. Marked converts hr’s and <!—BREAK—> by replacing it with a div with style "page-break-before:always". This only works when Marked is printing to PDF, it wouldn’t necessarily translate to an exported HTML file. You could easily add (or shell convert) your own divs and embed the styles directly, though.

    2. I’ve looked at solutions from Pandoc to wkhtmltopdf to wkpdf, but every time either size or license issues have been a problem. I’d consider adding an export processor, but there are so many options to consider when using those converters that it would require a lot of configuration on the user’s part. If you’re already capable of doing the conversion on the command line, it might just be prudent to export the html from Marked and have a Hazel process do the conversion via script…

    That being said, I’ll look back into wihtmltopdf and see if it’s a feasible option.

  8. 8 Posted by potsky on 25 Oct, 2013 02:55 PM

    potsky's Avatar

    Hi Brett,

    thank you very much, it works like a charm, I just create a build process in Sublime Text and PDF are now perfect with a TOC and with a footer/header !

    Just a curiosity : what do you use to convert the MD to HTML ? Pandoc ? If I could include this in my build keyboard shortcut, I will save a SAVE action in Marked :-)

  9. Support Staff 9 Posted by Brett on 25 Oct, 2013 02:59 PM

    Brett's Avatar

    I generally use Marked to do the conversion, but if I’m working from the command line I usually use MultiMarkdown or Kramdown.

    -Brett

  10. Support Staff 10 Posted by Brett on 25 Oct, 2013 02:59 PM

    Brett's Avatar

    Or did you mean _in_ Marked? Marked by default uses MultiMarkdown 4, with the option to use Discount instead.

    -Brett

  11. 11 Posted by potsky on 25 Oct, 2013 03:07 PM

    potsky's Avatar

    Sorry :-)
    I was talking about the Marked application...

    I would like to change our current workflow on Mac OS X :

    • I write some *.md in Sublime Text
    • When I save in ST, Marked is refreshed, perfect
    • I save as HTML in Marked
    • Then come back in ST to Build the PDF from the HTML generated by Marked

    I would like to have this workflow (especially for my colleagues) :

    • They write some *.md in Sublime Text
    • When they save in ST, Marked is refreshed, perfect
    • They Build the PDF in ST from the MD and not from the HTML generated by Marked

    The Build now commit, pull, merge and push in the git repository, send emails to alert people to read the updated document, etc...

  12. 12 Posted by potsky on 25 Oct, 2013 03:09 PM

    potsky's Avatar
  13. Support Staff 13 Posted by Brett on 25 Oct, 2013 07:55 PM

    Brett's Avatar

    Correct on MultiMarkdown 4.

    As far as the workflow goes, do you still want the output PDF to have Marked styles? If that’s the case, you’ll need to use a webkit-compatible PDF generator like wkhtmltopdf or wkpdf to do the build as a command line tool. You’ll need to export the CSS from your preferred or custom style and then include it via command line options with the CLI.

    Most of Marked’s features, such as Table of Contents and pagination tweaks aren’t available without Marked running. If your needs are simple, though, a wkhtmltopdf workflow with included CSS may work fine.

  14. 14 Posted by JR on 07 Apr, 2015 03:43 PM

    JR's Avatar

    I am wondering if this has been resolved. I'm using Marked Version 2.4.11 (895), and when I generate a paginated PDF, the links within the document are visible but do not correctly link. If it hasn't been fixed, is there a workaround that can be employed?

  15. Support Staff 15 Posted by Brett on 07 Apr, 2015 04:33 PM

    Brett's Avatar

    There has been no change to WebKit's handling of intra-document links at this point. The only workaround would still be to export to HTML and use wkhtlm2pdf, but the same problems with certain syntax not being carried over will exist.

  16. 16 Posted by Cai on 09 Aug, 2016 01:31 PM

    Cai's Avatar

    Yes, I would also like this. It's nice to put a table of contents at the start of PDF documentation created from markdown, but this is only useful with internal links.

  17. 17 Posted by Jon on 14 Oct, 2016 11:28 PM

    Jon's Avatar

    I too need internal links in pdfs. It's the only issue I have with an otherwise great app.

  18. 18 Posted by torsten.louland on 18 May, 2017 01:55 PM

    torsten.louland's Avatar

    Looks like internal links have finally been fixed in webkit:

    https://bugs.webkit.org/show_bug.cgi?id=112081

    and

        "Made printing to PDF produce internal links when the HTML has internal links"

    ...from https://developer.apple.com/safari/technology-preview/release-notes/

    Any chance you can integrate this Marked 2 early?

  19. Support Staff 19 Posted by Brett on 18 May, 2017 05:22 PM

    Brett's Avatar

    Yeah, that update to the webkit source was made almost 2 years ago now,
    but Apple still hasn't incorporated it into the SDK version. In order to
    implement it in Marked, I'd have to include a custom webkit, which would
    basically mean putting a whole copy of Safari into it. The download size
    would go up by 500% and it's just not feasible for me to maintain a
    codebase that's split off from the SDK.

    -Brett

  20. 20 Posted by torsten.louland on 18 May, 2017 08:29 PM

    torsten.louland's Avatar

    Hmmm...

    "Made printing to PDF produce internal links when the HTML has internal links" was in release 18 of the Safari technology preview and we're now at release 30. I downloaded it and gave File>Export as PDF… and File>Print…>Save as PDF… a try:
    - in a web page with plain unqualified #fragment links within the page, those links come out as URL links in the generated PDF, rather that internal PDF links (/GoTo actions) — bad :-(
    - when I open a local HTML file (generated by Marked 2 & containing internal links) the internal #fragment links remain as internal links in the generated PDF — good  the bug is indeed fixed.

    Then I cross-checked with latest Safari (10.1 on Sierra 10.12.4), and that gave the same behaviour. Does it uses its own more recent webkit? Hard to tell, given the obfuscation inside the bundle. Security-wise it would not make sense to have Safari use a different webkit to the rest of the system - that could allow Safari to have some security fixes that were not available to the rest of the system - they would never do that.

    So… is there a possibility that the ability to generate internal links in PDFs is already present in webkit on Sierra, and that Marked 2 is not for some reason benefiting from it? (Perhaps some trick needed to engage it.)

    Worth some digging and/or a radar?

  21. Support Staff 21 Posted by Brett on 24 May, 2017 04:23 PM

    Brett's Avatar

    Safari does use a different webkit than is available in the SDK, though
    I should note that Marked runs on the original WebKit and not the
    WKWebView that was introduced more recently. I'll take another look at
    switching that over and see if it makes a difference (it didn't
    previously, and it was a lot of re-coding to adopt the new API for
    that).

    -Brett

  22. 22 Posted by mantoine.mac on 12 Jul, 2017 04:13 PM

    mantoine.mac's Avatar

    Here's a simple workflow I came up with to work around Marked2 limitation.

    I post it here for others to use and (why not) improve. Plus (hopefully) it could provide new clues for Brett to implement internal links to PDF exports.

    Here it is :

    • export from Marked2 as HTML (Cmd-S)
    • open the resulting HTML file in your web browser of choice
    • Print to PDF...

    The resulting PDF includes fully functional internal links.

    This might not work with any browser though. I've successfully tested it with Safari, but I prefer Vivaldi (or Chrome) because it allows me to set page margins right into de print dialog.

    As an aside, I include page breaks in the Markdown document as HTML :

    <div style="page-break-after: always;"></div>
    

    There are other ways to achieve page breaks but this one doesn't require any modification to the CSS.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac