iainc / iA-Writer-Templates

Preview, create PDFs, and print documents in your own style with templates in iA Writer.

Home Page:https://ia.net/writer/templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PDF renders before template assets have finished loading

camfowler opened this issue · comments

I'm using some javascript to search for code blocks with a class "plantuml", then calling a web server and swapping the content with an SVG.

This works great when rendering as "Web". Works flawlessly.

When rendering as "PDF" it works most of the time. It works well enough that I can get a PDF out of it with multiple attempts. Much of the time, however it will render before the SVG has had time to be fetched, so the diagram displays the code block instead of the SVG. When the document has multiple diagrams, it is even more problematic to get a complete PDF.

Is there a way I could delay the rendering of the PDF until the SVG has had time to be retrieved? Perhaps a setting in Info.plist like IATemplatePDFRenderDelay? That way I could set it to a reasonable value to allow time for a template with slightly longer load times (say 500ms?) to fully render.

window.addEventListener('load', function() {
  document.body.addEventListener('ia-writer-change', function() {
    renderPlantUML();
  });
});

The fetch code is something like this:

function renderPlantUML(){
  var plantumls = document.querySelectorAll('.plantuml');
  plantumls.forEach(function (plantuml, index) {
    svg = compress(plantuml.innerText);
    plantuml.innerHTML = "loading...";

    fetch(svg)
      .then(r => r.text())
      .then(text => {
        plantuml.innerHTML = text;
      })
      .catch(console.error.bind(console));
  });
}

Some times it renders just the "loading..." text, and others it renders the SVG.

You could use a synchronous XMLHttpRequest instead of fetch (which is always async)