jgraph / drawio

draw.io is a JavaScript, client-side editor for general diagramming.
https://www.drawio.com
Other
40.73k stars 7.57k forks source link

Export layers to PDF #1561

Closed mararad closed 3 months ago

mararad commented 3 years ago

I've worked with autocad in the past and I remember exporting a drawing to PDF and still been able to enable and disable layers in it. This is very helpful to help visualize cluttered diagrams.

MC-24 commented 3 years ago

I agree. Please also consider an option to export layers to PDF (as separate files for each layer). I've been using this feature in OmniGraffle...

Screenshot 2021-01-12 at 20 29 01
acsr commented 3 years ago

It is definitely an important feature to preserve layers for roundtripping workflows. On the other hand, during import of an SVG exported eg. from Adobe Illustrator Draw.io will not keep existing layers as well. This should be possible to fix. If you need example files for testing, let me know.

Update: When exporting SVG from a layered Adobe Illustrator file in CS5 without "Preserve Editing" and reimporting again in AI CS5 the Layers are represented in named groups as well. No idea which kind of application specific markup is used to preserve the layers in eg. Inkscape SVG etc. (as long as private tags / attributes are used this may work just on application level) SVG from Illustrator can currently only be placed as a whole , not imported in an editable form.

ShonaMercie commented 2 years ago

I would really appreciate it if this was developed, as I'm really stuck how to do this :)

JanB1 commented 2 years ago

See also Discussion jgraph/drawio#2458. I support this feature. A user currently would have to resort to Visio or some other software to get this feature.

pierrebarroca commented 2 years ago

It would be very nice to have this feature!

schorsch0206 commented 1 year ago

It'd be really great if this feature could be implemented! Isn't there any voting functionality to show interest in feature requests raised by someone else alreay?

stale[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. See the FAQ for more information.

acsr commented 9 months ago

I would like to add another aspect to the PDF Layer export feature. If you take into account, that there is a native approach to export the layered PDF in one rush, by writing the necessary structures into the PDF, you can take another approach (with some drawbacks).

Scripted Export Approach

Scripted export as seperate layers and merging multiple PDF into one using one PDF per layer.

  1. Script draw.io to iterate through a list of all currently visible layers (omit invisible layers)
  2. Export every visible layer as a single PDF with a basename and an LayerOrderID and the Layername as suffix with delimiter. Save the files in the tmp location of the OS.
  3. Combine the exported PDFs into one Layered PDF using the given order by ID and the suffix als Layername using OpenSource commandline tools (my research on this has to be added later, due not reachable now from my place of writing).

The Watermark Trick

There is one trick you can use to add layers in PDF by using the watermark / background feature first. Watermarks are actually a simple layer at the top with adjustable transformation and tranparency with a special flag.

Watermarks can be transformed to a simple PDF layer by removing the flag.

This approach is similar to Python Scripts often used in Inkscape etc. to add features not possible with the default functionality but not worth a plugin or changing the general functionality.

Remark on export preserving object structures in SVG and PDF

It is not helpful that the layers are not included even in the SVG at least as groups. This would offer another approach for scripted conversion not relying on Draw.io itselv.

Inkscape can still open the SVG only without any preservation of layer structures. The Draw.io layer names are not available in the SVG XML tree or even reflected as nested structure.

terefang commented 3 months ago

i dont know what is so complicated to mark resources for pdf layers.

it is as simple as wrapping the regular output in the following:

/OC /L101 BDC q <content-stream> Q EDC

and creating a layer resource like that:

101 0 obj
/Type /OCG /Name (L101)
endobj

and adding the resource to the page "Properties" resources.

<<  /Type /Page  /Resources <<  /Properties <<  /L101 101 0 R>>  >>

-- T

davidjgraph commented 3 months ago

i dont know what is so complicated to mark resources for pdf layers.

Then submit a PR if it's that easy.

terefang commented 3 months ago

LOL

draw.io is also closed to contributions. We follow a development process compliant with our SOC 2 Type II process. We do not have a mechanism where we can accept contributions from non-staff members.

not allow by your own policies !

davidjgraph commented 3 months ago

I will make an exception for this case. Please submit the PR.

JanB1 commented 3 months ago

@terefang Yes please. You would be my/our hero!

terefang commented 3 months ago

need to first ask my company if this is allowed by contract.

terefang commented 3 months ago

nonetheless while awaiting confirmation, i searched the codebase to to look how you generate pdf.

from the package.json files it looks like you use pdf-lib from npmjs.

i also found the PDFDocument initializer, but then i am lost.

i only found the pdfToPDF method but this is not part of the pdf-lib api.

so can you point me in the right direction (and file) where pdf-export actually starts ?

my javascript is a little bit rusty, since i wrote the last js i remember in 2010.

alderg commented 3 months ago

We convert HTML to PDF on line 714

terefang commented 3 months ago

We convert HTML to PDF on line 714

that would mean it is "technically impossible" to implement layers because you are rendering thru the browser implementation and not some interfacing pdf library.

terefang commented 3 months ago

unless you change the rendering, of course !

acsr commented 3 months ago

@terefang

that would mean it is "technically impossible" to implement layers because you are rendering thru the browser implementation and not some interfacing pdf library. unless you change the rendering, of course ! Yes, unfortunately there is no documentationat all mentioning layers in the used library:

  • PDF-LIB · Create and modify PDF documents in any JavaScript environment. url:: https://Hopding.github.io/ abstract: Create and modify PDF documents in any JavaScript environment.

If you are searching for other soultions (to render), I want to mention the term Optional Content Group (OCG)used in a layer features in PDF documents to organize content.

For me it is unfortunately beyond my (current) scope to support this on the javascript side.

Thanks for picking this up.